scrapy重试机制_Python Scrapy不重试连接超时【多中间件】-程序员宅基地

技术标签: scrapy重试机制  

问题 (Question)

I've used some proxies to crawl some website. Here is I did in the settings.py:

# Retry many times since proxies often failRETRY_TIMES = 10# Retry on most error codes since proxies fail for different reasonsRETRY_HTTP_CODES = [500, 503, 504, 400, 403, 404, 408]DOWNLOAD_DELAY = 3 # 5,000 ms of delayDOWNLOADER_MIDDLEWARES = {

'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware' : None,

'myspider.comm.rotate_useragent.RotateUserAgentMiddleware' : 100,

'scrapy.contrib.downloadermiddleware.retry.RetryMiddleware': 200,

'myspider.comm.random_proxy.RandomProxyMiddleware': 300,

'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 400,

}

And I also have a proxy download middleware which have following methods:

def process_request(self, request, spider):

log('Requesting url %s with proxy %s...' % (request.url, proxy))def process_response(self, request, response, spider):

log('Response received from request url %s with proxy %s' % (request.url, proxy if proxy else 'nil'))def process_exception(self, request, exception, spider):

log_msg('Failed to request url %s with proxy %s with exception %s' % (request.url, proxy if proxy else 'nil', str(exception)))

#retry again.

return request

Since the proxy is not

As the before shows, I've set the settings RETRY_TIMES and RETRY_HTTP_CODES, and I've also return the request for a retry in the process_exception method of the proxy middle ware.

Why scrapy never retries for the failure request again, or how can I make sure the request is tried at least RETRY_TIMES I've set in the settings.py?

我使用了一些代理抓取一些网站。这是我在settings.py:

# Retry many times since proxies often failRETRY_TIMES = 10# Retry on most error codes since proxies fail for different reasonsRETRY_HTTP_CODES = [500, 503, 504, 400, 403, 404, 408]DOWNLOAD_DELAY = 3 # 5,000 ms of delayDOWNLOADER_MIDDLEWARES = {

'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware' : None,

'myspider.comm.rotate_useragent.RotateUserAgentMiddleware' : 100,

'scrapy.contrib.downloadermiddleware.retry.RetryMiddleware': 200,

'myspider.comm.random_proxy.RandomProxyMiddleware': 300,

'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 400,

}

我也有一个代理下载

def process_request(self, request, spider):

log('Requesting url %s with proxy %s...' % (request.url, proxy))def process_response(self, request, response, spider):

log('Response received from request url %s with proxy %s' % (request.url, proxy if proxy else 'nil'))def process_exception(self, request, exception, spider):

log_msg('Failed to request url %s with proxy %s with exception %s' % (request.url, proxy if proxy else 'nil', str(exception)))

#retry again.

return request

有时因为代理不是非常稳定,process_exception经常提示请求失败消息。这里的问题是,没有再次尝试失败的请求。

如之前所示,我设置设置RETRY_TIMES和RETRY_HTTP_CODES,我还返回重试请求process_exception

为什么scrapy不再重试失败的请求,或如何确保请求是我试着至少RETRY_TIMES settings.py设置?

最佳答案 (Best Answer)

Thanks for the help

'scrapy.contrib.downloadermiddleware.retry.RetryMiddleware': 200,

'myspider.comm.random_proxy.RandomProxyMiddleware': 300,

Here Retry middleware gets run first, so it will retry the request before it makes it to the Proxy middleware. In my situation, scrapy needs the proxies to crawl the website, or it will timeout endlessly.

So I've reverse the priority between these two download middle wares:

'scrapy.contrib.downloadermiddleware.retry.RetryMiddleware': 300,

'myspider.comm.random_proxy.RandomProxyMiddleware': 200,

谢谢你的帮助从@nyov Scrapy IRC频道。

scrapy.contrib.downloadermiddleware.retry。RetryMiddleware”:200年,

“myspider.comm.random_proxy.RandomProxyMiddleware”:300年,

这里重试中间件运行第一,所以它将重试请求之前代理中间件。���我的情况下,scrapy需要爬行网站的代理,或者它将超时没完没了地。

所以我改变这两个下载中间产品之间的优先级:

scrapy.contrib.downloadermiddleware.retry。RetryMiddleware”:300年,

“myspider.comm.random_proxy.RandomProxyMiddleware”:200年,

答案 (Answer) 2

it seem that your proxy download middleware -> process_response is not playing by the rules and hence breaking the middlewares chain

process_response() should either: return a Response object, return a Request object or raise a IgnoreRequest exception.

If it returns a Response (it could be the same given response, or a brand-new one), that response will continue to be processed with the process_response() of the next middleware in the chain.

...

看起来,你的process_response不按规则玩,因此打破了

process_response():返回一个响应对象,返回一个请求对象或提高IgnoreRequest异常。

如果它返回一个响应(可以是相同的反应,或一个全新的),响应将继续处理与process_response链中的下一个

...

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42152298/article/details/111886728

智能推荐

消息中间件学习总结(14)——你的分布式应用真的需要那么多同步调用么?_中间件系统如何让其他人调用-程序员宅基地

文章浏览阅读2.3k次。摘要: 在5月17日举办的2016云栖大会·武汉峰会上阿里中间件产品专家马雷(阿仁)就阿里中间件MQ做了精彩的演讲,告诉大家:阿里中间件团队的目标是让消息“传”无边界。本文也就为什么使用消息中间件,消息中间件的核心场景进行了分享。相信阿仁的分享会让大家对分布式应用的异步调用有更加深刻的了解。精彩不要错过!对整个分布式应用系统而言,一共分为两种调用,一种是同步调用,一种是异步调用。同步调用就..._中间件系统如何让其他人调用

关于Linux的c编译器和汇编器_linux c编译工具-程序员宅基地

文章浏览阅读3.1k次。本宝宝是编程菜鸟,这两天刚好学到Linux的c编译器想谈谈我的心得,Linux老手可绕过,欢迎一起交流,共同进步!关于C语言的编译:打开终端用命令新建一个c语言文件,在文件上可以输入一个简单的c语言程序,主要是看它能编译就好,无特别意义。如下图所示:输入命令有显示***就可以了。关于安装MPlayer我先说说我遇到问题吧,我用的虚拟机版本是2013年发布的10.0.1_linux c编译工具

Ant Design Charts 仪表盘配置属性结合案例详细说明_ant design charts仪表盘-程序员宅基地

文章浏览阅读1.7k次。本次案例为仪表板,最终成品样式如下,案例种用到仪表盘分大部分属性,每个属性都注释说明作用。_ant design charts仪表盘

leetcode424. 替换后的最长重复字符-程序员宅基地

文章浏览阅读77次。做法双指针,暴力class Solution { public int characterReplacement(String s, int k) { if (s.equals("")) return 0; int n = s.length(); int Max = 0; Map<Character, Integer> m = new HashMap<>(); Deque<Charact

用python实现(Fibonacci sequence)斐波那契数列的三种方法_python求斐波那契数列-程序员宅基地

文章浏览阅读1.3k次,点赞19次,收藏12次。斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……_python求斐波那契数列

<s:fielderror/>-程序员宅基地

文章浏览阅读153次。1.s:fielderror输出指定错误信息,而不是输出全部错误信息在struts2框架中,例如在完成数据类型转换、数据校验时如果出现错误信息,我们会利用ActionSupport类中addFieldError方法来添加错误信息内容(如:在Action中使用this.addFieldError("username" , "用户名已存在!"); )。在JSP页面中利用..._s:fielderror

随便推点

Cannot use median strategy with non-numeric data: could not convert string to float: ‘<1_valueerror: cannot use mean strategy with non-nume-程序员宅基地

文章浏览阅读3.6k次。创建一个imputer实例, 指定你要用属性中的中位数替代该属性的缺失值,再使用fit()方法将imputer实例适配到训练集,实现代码如下:from sklearn.impute import SimpleImputerimputer = SimpleImputer(strategy='median')imputer.fit(housing)运行结果如下:ValueError: Cannot use median strategy with non-numeric data:could n_valueerror: cannot use mean strategy with non-numeric data: could not conver

tars源码漫谈第47篇------tc_mem_chunk.h/tc_mem_chunk.cpp(内存块操作)-程序员宅基地

文章浏览阅读5k次,点赞2次,收藏3次。跟上次介绍的tc_malloc_chunk类似,tc_mem_chunk中也是与内存相关的操作, 涉及到申请, 拷贝, 释放等等等等, 后续tars源码中需要用的时候再来看吧, 这次仅仅看看头文件:/** * Tencent is pleased to support the open source community by making Tars available. * ..._mem_chunk

XEIM飞鸽传书部分源码_linuc实现飞鸽传书建立连接-程序员宅基地

文章浏览阅读1.5k次。// xeimDlg.cpp : implementation file//#include "stdafx.h"#include "xeim.h"#include "xeimDlg.h"#include "XEIM_Message.h"#include "XEIM_User.h"#include using namespace std;#ifdef _DEBUG#define n_linuc实现飞鸽传书建立连接

数据挖掘实验-主成分分析与类特征化_主成分分析的目的和作用-程序员宅基地

文章浏览阅读2.1k次,点赞2次,收藏9次。数据挖掘实验-主成分分析与类特征化_主成分分析的目的和作用

实例讲解基于 React+Redux 的前端开发流程_基于react的前端开发流程-程序员宅基地

文章浏览阅读624次。前言:在当下的前端界,react 和 redux 发展得如火如荼,react 在 github 的 star 数达 42000 +,超过了 jquery 的 39000+,也即将超过前几年比较火的angular 1 的 49000+;redux 的 star 数也要接近 20000,可见大家对其的热情程度,究竟是什么魔力让大家为之疯狂呢?让我们上车,亲自体验一波试试~~本文章偏向于讲解redux流_基于react的前端开发流程

pytorch系列 -- 9 pytorch nn.init 中实现的初始化函数 uniform, normal, const, Xavier, He initialization_torch.nn.init.xavier_normal_和torch.nn.init.normal_-程序员宅基地

文章浏览阅读2.3k次,点赞2次,收藏27次。本文内容:nn.init 中各种初始化函数Xavier 初始化He 初始化torch.init https://pytorch.org/docs/stable/nn.html#torch-nn-init1. 均匀分布torch.nn.init.uniform_(tensor, a=0, b=1)2. 正太分布torch.nn.init.normal_(tensor, mea..._torch.nn.init.xavier_normal_和torch.nn.init.normal_区别

推荐文章

热门文章

相关标签