首页 > 其他分享 >scray中的Request 不执行回调

scray中的Request 不执行回调

时间:2022-11-21 12:02:20浏览次数:60  
标签:scray attribute middleware Request offsite scrapy allowed domains 回调


在 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​​​​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​

如下摘自手册

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.





在 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​​​​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​

如下摘自手册

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.

标签:scray,attribute,middleware,Request,offsite,scrapy,allowed,domains,回调
From: https://blog.51cto.com/u_15882671/5873324

相关文章