使用scrapy做爬虫遇到的一些坑:调试成功但是没有办法输出想要的结果(request的回调函数不执行)(url去重)dont_filter=True


可以看到,当parse的第一个断点设置在第54行时能运行成功。

接下来将断点设置在parse_news函数中。



照理来说应该会正常输出item_1的内容,但是为什么没有办法正确输出呢?而且也没有报错啊!

调试的时候,发现回调函数 parse_detail 没有被调用,这可能就是被过滤掉了,查看 scrapy 的输出日志 offsite/filtered 会显示过滤的数目。


因为被去重过滤了,所以才调试不了啊!

既然知道是因为被过滤那问题就好解决了。

scrapy会对request的URL去重(RFPDupeFilter),加上dont_filter则告诉它这个URL不参与去重。

两种方法能够使 requests 不被过滤: 
1. 在 allowed_domains 中加入 url 

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

在scrapy的文档中就有提及这个问题

https://doc.scrapy.org/en/latest/faq.html?highlight=offsite%2Ffiltered
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

所以接下来我们改一下


然后答案就出现了,哈哈哈,顿时觉得爬虫很好玩。


猜你喜欢

转载自blog.csdn.net/weixin_41931602/article/details/80397192