In: Computer Science
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:
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.