mardi 5 mai 2020

Error in python web scraper wont run properly

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?Tpk=graphics%20cards'

# opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

#html parsing
page_soup = soup(page_html, "html.parser")

#grabs each product
containers =  page_soup.findAll("div", {"class":"item-container"})

for container in containers:
    brand = container[0].img["title"].title()

    title_container = container.findAll("a", {"class":"item-title"})
    product_name = title_container[0].txt


    shipping_container = container.findAll("li", {"class":"price-ship"})
    shipping = shipping_container[0].text.strip()


    print("Brand: "+ brand)
    print("product name: "+ product_name)
    print("shipping: "+ shipping)

After I run this program it gives me the following errors.

Traceback (most recent call last): File "my_first_websraper.py", line 18, in brand = container[0].img["title"].title() File "C:\Users\MyUserName\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bs4\element.py", line 1368, in getitem return self.attrs[key] KeyError: 0

When he runs it in the tutorial it not only lists everything properly but it lists everything on the website the same way. Any ideas on how to solve this issue?

For an idea of how it should look got to 28:55 on this video: https://www.youtube.com/watch?v=XQgXKtPSzUI




Aucun commentaire:

Enregistrer un commentaire