Jul-25-2020, 03:45 PM
The code is supposed to open 5 links e new tabs but it's given a error.
The code was written bellow:
The code was written bellow:
#! python3 # lucky.py - Opens several Google search results. import requests, sys, webbrowser, bs4 print('Googling...') # display text while downloading the Google page res = requests.get('http://google.com/search?q=python' + ' '.join(sys.argv)) res.raise_for_status() print(sys.argv[1:]) # Retrieve top search result links. soup = bs4.BeautifulSoup(res.text, features="html.parser") # Open a browser tab for each result. linkElems = soup.select('.r a') # Open a browser tab for each result. link_elems = soup.select('.r a') numOpen = max(5, len(linkElems)) print(numOpen) for i in range(numOpen): webbrowser.open('http://google.com/search?q=' + link_elems[i].get('href'))This is the error
Error:Traceback (most recent call last):
File "C:/Users/-/PycharmProjects/guppe/testandohtml.py", line 17, in <module>
webbrowser.open('http://google.com/search?q=' + link_elems[i].get('href'))
IndexError: list index out of range
The exemple is from the book : 'Automate the Boring Stuff with Python', Thanks