簡體   English   中英

當我運行它時,我的Python代碼不斷返回不同的錯誤

[英]My Python code keeps returning different errors when i run it

我正在用Python創建一個小型的網絡抓取程序,該程序從newegg.com獲取GPU信息並記下所有價格。
截至目前,我尚未實現電子表格,因為每次運行該電子表格時,都會收到2個錯誤之一。

代碼如下:

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

myURL = "https://www.newegg.com/global/uk/Product/ProductList.aspx?Submit=ENE&N=-1&IsNodeId=1&Description=graphics%20card&bop=And&PageSize=96&order=BESTMATCH" # defining my url as a variable

uClient = uReq(myURL) #opening the connection
page_html = uClient.read() # getting html
uClient.close() # closing the client

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

containers = page_soup.findAll("div", {"class":"item-container"}) #get all 
item containers/product

container = containers[0]

count = 0

for container in containers:

    print(count)

    brand = container.div.div.a.img["title"]# get the brand of the card
    if brand == None:
        print("N/A")
    else:
        print(brand)

    title_container = container.findAll("a", {"class", "item-title"})
    product_name = title_container[0].text # getting the product name
    if product_name == None:
        print("N/A")
    else:
        print(product_name)

    price1 = container.find("div",{"class":"item-action"})
    price1 = price1.ul
    price2 = price1.find("li", {"class": "price-current"}).contents #defining the product price
    if not price2:
        print("N/A")
    else:
        print(price2[2])
        print(price2[3].text)
        print(price2[4].text) 

    print()
    count+=1

錯誤說明如下:

  1. 追溯(最近一次通話):文件“ C:/ Users / Ethan Price / Desktop / test.py”,第23行,品牌= container.div.div.a.img [“ title”]#獲得品牌卡TypeError:“ NoneType”對象不可下標

  2. 追溯(最近一次通話最近):文件“ C:/ Users / Ethan Price / Desktop / test.py”,行43,在print(price2 [2])中IndexError:列表索引超出范圍

在嘗試修復它時,我嘗試將列表變成數組,並嘗試更改if語句。

這兩條錯誤消息都意味着您希望看到的某些元素不存在。 首先是抱怨當您嘗試對其進行下標時container.div.div.a.imgNone (出於明顯的原因, None不能下標)。 另一個人抱怨列表price2時間不像您想的那么長,因此price2[2]超出范圍。

第一個錯誤,檢查圖像及其標題標記是否存在

brand = None
# might want to check there is even an anchor tag 
_img = container.div.div.a.img
if _img:
    brand = _img["title"]

二,檢查價目表的長度

If 2 <= len(price2) <= 5:
    for p in price2[2:]
        print(p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM