我已经制作了一个抓取工具来解析亚马逊的产品名称,但是当我运行我的抓取工具时,它既不会带来任何结果,也不会显示任何错误。到目前为止我知道,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()
答案 0 :(得分:2)
您没有收到任何错误,因为您的脚本中有一个try - except块 如果要显示错误,请更改:
except:
continue
到:
except Exception as e :
print(e.message)
continue
注意:
如果您打算单独处理这些情况,最好为每个预期的异常(keyerror,valueerror等)设置一个except块。
感谢@David Metcalfe提出此建议