欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用requests爬取报错“Max retries exceeded with url“的解决方法

程序员文章站 2022-03-03 21:28:19
某次在写爬虫时,运行之后报错requests.exceptions.ProxyError:HTTPSConnectionPool(host=‘xxx.xxx.xxx’, port=443):Max retries exceeded with url: xxxxxxx (Caused by ProxyError(‘Cannot connect to proxy.’, NewConnectionError(’

某次在写爬虫时,运行之后报错

requests.exceptions.ProxyError:
HTTPSConnectionPool(host=‘xxx.xxx.xxx’, port=443):
Max retries exceeded with url: xxxxxxx (Caused by ProxyError
(‘Cannot connect to proxy.’, NewConnectionError(’<urllib3.connection.HTTPSConnection object at 0x000001EF209B1D30>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。’)))

原因:

  1. http连接太多没有关闭导致的
  2. 可能是访问过于频繁,使用IP代理即可

使用IP代理参考以下文章
Python爬虫避坑IP代理教程避坑(reuqests和selenium的ip代理)

解决办法

1、增加重试连接次数
requests.DEFAULT_RETRIES = 5
s = requests.session()
2、关闭多余的连接
s.keep_alive = False

requests使用了urllib3库,默认的http connection是keep-alive的,requests设置False关闭。

如果对大家有帮助,可以点赞关注和收藏一下哦,谢谢各位!

博主更多博客文章

本文地址:https://blog.csdn.net/llllllkkkkkooooo/article/details/107637980