【问题标题】:Process page that returns HTTP 500 code with Python/Scrapy使用 Python/Scrapy 返回 HTTP 500 代码的进程页面
【发布时间】:2020-04-10 15:21:43
【问题描述】:

我无法访问一些返回 HTTP 500 代码以及格式正确的 HTML 页面的网站。

所以,我可以用 Chorme/Firefox 下载页面,但我不能用 Scrapy。

抓取日志:

2020-04-10 15:57:16 [scrapy.core.engine] INFO: Spider opened
2020-04-10 15:57:16 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-04-10 15:57:16 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-04-10 15:57:18 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://www.industrialmotors.com/products/toshiba-motors/where/p/1.html> (failed 1 times): 500 Internal Server Error
2020-04-10 15:57:19 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://www.industrialmotors.com/products/toshiba-motors/where/p/1.html> (failed 2 times): 500 Internal Server Error
2020-04-10 15:57:20 [scrapy.downloadermiddlewares.retry] ERROR: Gave up retrying <GET https://www.industrialmotors.com/products/toshiba-motors/where/p/1.html> (failed 3 times): 500 Internal Server Error
2020-04-10 15:57:20 [scrapy.core.engine] DEBUG: Crawled (500) <GET https://www.industrialmotors.com/products/toshiba-motors/where/p/1.html> (referer: None)
2020-04-10 15:57:20 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <500 https://www.industrialmotors.com/products/toshiba-motors/where/p/1.html>: HTTP status code is not handled or not allowed

请看下面的屏幕截图,显示网络服务器返回 HTTP 500 以及在 Firefox 中正确呈现的网页。

测试页面是https://www.industrialmotors.com/products/toshiba-motors/where/p/1.html

谢谢,如果我应该添加任何细节,请告诉我。

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    如果你只想在一只蜘蛛上处理这个问题:

    class MySpider(Spider):
        ...
        handle_httpstatus_list = [500]
    

    如果您只想处理一个请求:

    ...
    def my_parse_method(self, response):
        ...
        yield Request(url='http://example.com', meta={'handle_httpstatus_list': [500]})
    

    【讨论】:

      【解决方案2】:

      默认情况下,scrapy 会忽略500 状态码并且不处理它的响应。但是你可以通过在你的蜘蛛类中指定它来覆盖这个设置。

      类似这样的:

      class YourSpider:
          custom_settings = {
              'HTTPERROR_ALLOWED_CODES': [500]
          }
      

      更多信息here

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        相关资源
        最近更新 更多