【问题标题】:Scrapy :: How to get requests with exceptions to export to .csv?Scrapy :: 如何获取例外请求以导出到.csv?
【发布时间】:2019-06-21 13:56:48
【问题描述】:

我对使用 Scrapy 还很陌生,并且已经编写了大约 2 年的代码(抱歉,如果这是一个愚蠢的问题)。

我目前正在尝试抓取一般信息,例如网站是否在网站列表中具有“隐私政策”链接或“关于我们”链接。我已经能够在具有支持 HTTPS 的 URL 或具有实时链接的网站上抓取信息。

对于无法加载或存在 HTTPS 与 HTTP 问题的网站,我遇到了一些例外情况:

  • twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion: Connection lost.>]

  • twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_record', 'wrong version number')]>]

根据蜘蛛的多次爬取,我发现生成的.csv 的网站排除了这些链接。

我想知道如果可能的话,如何让蜘蛛将这些失败的链接包含在每列的预设参数中。

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    在Request函数中除了callback还有errback(文档是here)。

    您可以编写函数来处理产生错误的请求。

    所以你使用:yield Request(url="http://www.example.com", callback=self.mycallback, errback=self.myerrback)

    并定义:

    def myerrback(self, failure):
        # your processing here
    

    检查使用情况here。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2015-10-19
      相关资源
      最近更新 更多