Scrapy: catch responses with specific HTTP server codes

后端 未结 1 792
太阳男子
太阳男子 2021-02-06 03:39

We have a pretty much standard Scrapy project (Scrapy 0.24).

I\'d like to catch specific HTTP response codes, such as 200, 500, 502, 503, 504 etc.

Something like

相关标签:
1条回答
  • 2021-02-06 04:27

    By default, Scrapy only handles responses with status codes 200-300.

    Let Scrapy handle 500 and 502:

    class Spider(...):
        handle_httpstatus_list = [500, 502]
    

    Then, in the parse() callback, check response.status:

    def parse(response):
        if response.status == 500:
            # logic here
        elif response.status == 502:
            # logic here
    
    0 讨论(0)
提交回复
热议问题