[英]Why my crawler neither fetches any data nor throws any error
我制作了一個搜尋器來解析來自亞馬遜的產品名稱,但是當我運行搜尋器時,它既不會帶來任何結果,也不會顯示任何錯誤。 到目前為止,我知道Xpaths還可以。 找不到我已經犯的任何錯誤。 希望有人來研究它。
import requests
from lxml import html
def Startpoint():
url = "https://www.amazon.com/Best-Sellers/zgbs"
response = requests.get(url)
tree = html.fromstring(response.text)
titles = tree.xpath('//ul[@id="zg_browseRoot"]')
for title in titles:
items=title.xpath('.//li/a/@href')
for item in items:
Endpoint(item)
def Endpoint(links):
response = requests.get(links)
tree = html.fromstring(response.text)
titles = tree.xpath('//div[@class="a-section a-spacing-none p13n-asin"]')
for title in titles:
try :
Name=title.xpath('.//div[@class="p13n-sc-truncated-hyphen p13n-sc-truncated"]/text()')[0]
print(Name)
except:
continue
Startpoint()
您不會遇到任何錯誤,因為腳本中包含try-except塊。
如果要顯示錯誤,請更改此:
except:
continue
至 :
except Exception as e :
print(e.message)
continue
注意 :
如果您打算分別處理這些情況,則最好為每個預期的異常(keyerror,valueerror等)都具有一個except塊。
感謝@David Metcalfe的建議
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.