Python 为什么我的爬虫既不获取任何数据也不抛出任何错误

Python 为什么我的爬虫既不获取任何数据也不抛出任何错误,python,web-scraping,web-crawler,Python,Web Scraping,Web Crawler,我制作了一个爬虫来解析来自Amazon的产品名称,但当我运行我的爬虫时,它既不会带来任何结果,也不会显示任何错误。据我所知,XPath还可以。找不出我已经犯过的任何错误。希望有人调查一下 import requests from lxml import html def Startpoint(): url = "https://www.amazon.com/Best-Sellers/zgbs" response = requests.get(url) tree = ht

我制作了一个爬虫来解析来自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()

您不会得到任何错误,因为您的脚本中有try-except块。
如果要显示错误,请更改以下内容:

except:
    continue
致:

except Exception as e : 
    print(e.message)
    continue
注:

如果您计划单独处理这些情况,最好为每个预期异常(keyerror、valueerror等)设置一个exception块


感谢@David Metcalfe的这一建议

您不会收到任何错误,因为您的脚本中有一个try-Exception块。
如果要显示错误,请更改以下内容:

except:
    continue
致:

except Exception as e : 
    print(e.message)
    continue
注:

如果您计划单独处理这些情况,最好为每个预期异常(keyerror、valueerror等)设置一个exception块


感谢@David Metcalfe的这一建议

我想不起来也找不到政治公众人物,但我确信在某个地方也提到过,除了《OP》现在看到的原因之外,采用全面的
是非常不明智的。如果你能找到它,答案中可能值得一提。@David Metcalfe如果你的意思是有一个
,除了多个可能的例外,我同意。这篇文章旨在帮助OP调试他的脚本,这不是一个最佳实践示例。我想不起或找不到PEP,但我肯定在某个地方也提到过,除了OP现在看到的原因之外,使用全面的
是非常不明智的。如果你能找到它,答案中可能值得一提。@David Metcalfe如果你的意思是有一个
,除了多个可能的例外,我同意。这篇文章旨在帮助OP调试他的脚本,它不是一个最佳实践示例