Question

In: Computer Science

The following .py script takes a users input of search parameters on command line, creates a...

The following .py script takes a users input of search parameters on command line, creates a custom search URL, then returns top 5 result links from Google Scholar.
Alter the below code so that the script opens up each found link in a separate browser window (or tabbed windows in a single browser).

——.py

import requests, sys, webbrowser
from bs4 import BeautifulSoup
print("Searching for " + "+".join(sys.argv[1:]))
myres = requests.get("https://scholar.google.com/scholar?
hl=en&q=" + "+".join(sys.argv[1:]) + "&*")
soup = BeautifulSoup(myres.text, "html.parser")
elems = soup.select(".gs_rt a")
num = min(len(elems), 5)
for i in range(num):
print(elems[i].get("href"))

Solutions

Expert Solution

You have already imported the webbrowser module to get the functionality for opening web URLs.

The only thing is calling the webbrowser module method. webbrowser module provides methods to open URLs in default web browser. Three methods are:

  1. webbrowser.open()
  2. webbrowser.open_new()
  3. webbrowser.open_new_tab()

All methods uses the default browser to open the URL and tries to use the same window to open the URL.

Code syntax to call webbrowser.open() method:

webbrowser.open(URL_HERE)

Fit the above method in the existing code:

import requests, sys, webbrowser
from bs4 import BeautifulSoup

print("Searching for " + "+".join(sys.argv[1:]))

myres = requests.get("https://scholar.google.com/scholar?hl=en&q=" + "+".join(sys.argv[1:]) + "&*")
soup = BeautifulSoup(myres.text, "html.parser")
elems = soup.select(".gs_rt a")
num = min(len(elems), 5)

for i in range(num):
print(elems[i].get("href"))
webbrowser.open(elems[i].get("href"))

Highlighted the change required to open the URL received in search in web browser.

Output on console:

5 new tab are opened in the browser window with the above URLs.


Related Solutions

IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
The following code takes in a command line argument and copies it into a buffer before...
The following code takes in a command line argument and copies it into a buffer before working on it. Explain in detail any problems that you see in the following C code and how you would fix it. Note only describe how you would fix it, do not actually rewrite or give me another version of the code. void bad_function(char *input) { char dest_buffer[32]; char input_len = strlen(input); if (input_len < 32) { strcpy(dest_buffer, input_len); printf(“Command line argument is %s.\n”,dest_buffer);...
• Develop an "Echo" command that returns the parameters on different lines. Example: For input "$...
• Develop an "Echo" command that returns the parameters on different lines. Example: For input "$ Java Echo Hello world!" The answer will be: "Hello world ! ” using java programming language
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result.
Write a Python script that takes an input image and output's the name of the dominant...
Write a Python script that takes an input image and output's the name of the dominant color in that image(i.e. red, green, blue).  
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the items and calculates the tax due and the total due. The number of inputs is not known. You can assume that the input is always valid. (i.e no negative numbers or string like "cat"). Use a loop that takes the prices of items as parameters that are floats, counts the number of items, and sums them to find the total. You must also use...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the items and calculates the tax due and the total due. The number of inputs is not known. You can assume that the input is always valid. (i.e no negative numbers or string like "cat"). Use a loop that takes the prices of items as parameters that are floats, counts the number of items, and sums them to find the total. You must also use...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the...
Write a script called script2-4.py that takes a person's delivery order as inputs, totals all the items and calculates the tax due and the total due. The number of inputs is not known. You can assume that the input is always valid. (i.e no negative numbers or string like "cat"). Use a loop that takes the prices of items as parameters that are floats, counts the number of items, and sums them to find the total. You must also use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT