我制作了一个爬虫来解析来自Amazon的产品的名称,但是当我运行我的爬虫时,它既不会带来任何结果,也不会显示任何错误。到目前为止我知道Xpath还行。找不到我已经犯过的任何错误。希望有人来调查。
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()发布于 2017-05-01 21:14:01
您不会得到任何错误,因为您有一个尝试-除了块在您的脚本。
如果要显示错误,请更改以下内容:
except:
continue致:
except Exception as e :
print(e.message)
continue注意:
如果计划单独处理这些情况,最好为每个预期异常( keyerror、valueerror等)设置一个etc块。
感谢@David Metcalfe给出的建议
https://stackoverflow.com/questions/43726322
复制相似问题