from urllib import request
from bs4 import BeautifulSoup
page_url = "http://www.newegg.com/Product/ProductList.aspx?Submit=ENE&N=-1&IsNodeId=1&Description=GTX&bop=And&Page=1&PageSize=36&order=BESTMATCH"
uclient = request.urlopen(page_url) #open a webclient
html_page = uclient.read()
page_soup = BeautifulSoup(html_page,"html.parser")
uclient.close()
containers = page_soup.find_all("div",{"class" :"item-cell"})
title_list = []
for contain in containers:
title = contain.select("img")[0]["title"]
print(title)# for troubleshooting
print(len(title_list)) #for troubleshooting
title_list.append(title)
print(title_list)
Can someone pls help in troubleshooting? Everytime i run the code , once it returns 12 values sometimes 28 , sometimes 30 and then it gives an error .:
Input In [67], in <cell line: 16>()
15 title_list = []
16 for contain in containers:
---> 17 title = contain.select("img")[0]["title"]
18 print(title)# for troubleshooting
19 print(len(title_list)) #for troubleshooting
File C:\ProgramData\Anaconda3\lib\site-packages\bs4\element.py:1519, in Tag.__getitem__(self, key)
1516 def __getitem__(self, key):
1517 """tag[key] returns the value of the 'key' attribute for the Tag,
1518 and throws an exception if it's not there."""
-> 1519 return self.attrs[key]
KeyError: 'title'