Jul-20-2018, 10:40 PM
This is web scraping project from Automate the Boring Stuff with Python. From some reason solution doesn't work and I have some doubts myself. The goal is to type a search term on the command line and have my computer automatically open a browser with all the top search results in new tabs.
#! python3
# lucky.py - Opens several Google search results.
import webbrowser, bs4, requests, sys
print('Googling...') # display text while downloading the Google page
res = requests.get('http://google.com/search?source=' + ' '.join(sys.argv[1:]))
print(res.raise_for_status())
# Retreive top search result links.
soup = bs4.BeautifulSoup(res.text, "html.parser")
# Open a browser tab for each result
linkElems = soup.select('.r a')
numOpen = min(5, len(linkElems))
for i in range(numOpen):
webbrowser.open('http://google.com' + linkElems[i].get('href'))I'm also not sure how to start this program. Should I type a search term in command prompt where I usually start my python files?
