您的位置:首页 > 产品设计 > UI/UE

scrapy - Request 中的回调函数不执行

2017-07-19 23:57 197 查看
推荐我的个人博客 http://blog.wuzhenyu.com.cn

在 scrapy 中,

scrapy.Request(url, headers=self.header, callback=self.parse_detail)


调试的时候,发现回调函数
parse_detail
没有被调用,这可能就是被过滤掉了,查看 scrapy 的输出日志
offsite/filtered
会显示过滤的数目。这个问题如何解决呢,查看手册发现(https://doc.scrapy.org/en/latest/faq.html?highlight=offsite%2Ffiltered)这个问题,这些日志信息都是由 scrapy 中的一个 middleware 抛出的,如果没有自定义,那么这个 middleware 就是默认的
Offsite Spider Middleware
,它的目的就是过滤掉那些不在
allowed_domains
列表中的请求 requests。

再次查看手册中关于
OffsiteMiddleware
的部分(https://doc.scrapy.org/en/latest/topics/spider-middleware.html#scrapy.spidermiddlewares.offsite.OffsiteMiddleware)

两种方法能够使 requests 不被过滤:

1. 在
allowed_domains
中加入 url

2. 在 scrapy.Request() 函数中将参数
dont_filter=True
设置为 True

如下摘自手册

If the spider doesn’t define an allowed_domains attribute, or the attribute is empty, the offsite middleware will allow all requests.

If the request has the dont_filter attribute set, the offsite middleware will allow the request even if its domain is not listed in allowed domains.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  callback parse scrapy