Scrapy:使用特定的 HTTP 服务器代码捕获响应 [英] Scrapy: catch responses with specific HTTP server codes

查看:50
本文介绍了Scrapy:使用特定的 HTTP 服务器代码捕获响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个非常标准的 Scrapy 项目(Scrapy 0.24).

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

我想捕获特定的 HTTP 响应代码,例如 200、500、502、503、504 等.

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

类似的东西:

class Spider(...):

    def parse(...):
        processes HTTP 200

    def parse_500(...):
        processes HTTP 500 errors

    def parse_502(...):
        processes HTTP 502 errors

    ...

我们该怎么做?

推荐答案

默认情况下,Scrapy 只处理状态码为 200-300 的响应.

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

让 Scrapy 处理 500502:

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

然后,在parse()回调中,检查response.status:

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

def parse(response):
    if response.status == 500:
        # logic here
    elif response.status == 502:
        # logic here

这篇关于Scrapy:使用特定的 HTTP 服务器代码捕获响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆