在使用代理IP做爬虫的时候,经常会遇到“Max retries exceeded with url…”这个问题,而且还不止一种,我们一起来看看常见的错误有哪几种,问题原因和解决方法是什么。
一、HTTPConnectionPool(host='www.***.com', port=80): Max retries exceeded with url: /……(Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x105b9d210>: Failed to establish a new connection: [Errno 65] No route to host',))
问题原因:http连接太多没有关闭导致的。
解决方法:
1、增加重试连接次数
requests.adapters.DEFAULT_RETRIES = 5
2、关闭多余的连接
requests使用了urllib3库,默认的http connection是keep-alive的,requests设置False关闭。
二、 requests.exceptions.SSLError: HTTPSConnectionPool(host='123.45.67.89', port=1234): Max retries exceeded with url: http://***.com/ (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'ssl3_get_record', 'wrong version number')])")))
问题原因:使用的是http代理IP,没有进行SSL加密
解决方法:使用https代理IP,比如站大爷的短效优质代理,长效住宅代理,独享IP池,合租IP池,线程IP池。
三、requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)", ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))
问题原因:该代理IP在抓取验证过程中被判定为有效,但是在使用的时候已经超过生命周期
解决方法:更换新的代理IP。
四、requests.exceptions.ProxyError: HTTPSConnectionPool(host='123.45.67.89', port=12580): Max retries exceeded with url: http://***.com/ (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 503 Too many open connections')))
问题原因:访问http网址,但强行使用https协议
解决方法:访问http网址,使用http协议;访问https网址,使用https协议。
以上是四种比较常见的“Max retries exceeded with url…”问题,当大家也碰到这个问题时,希望这篇文章对大家有所帮助。