Python requests“Max retries exceeded with url” error

今天写python网络爬虫的时候遇到一个问题,报错的具体内容如下:

HTTPConnectionPool(host='dds.cr.usgs.gov', port=80): Max retries exceeded with url: /ltaauth//sno18/ops/l1/2016/138/037/LC81380372016038LGN00.tar.gz?id=stfb9e0bgrpmc4j9lcg45ikrj1&iid=LC81380372016038LGN00&did=227966479&ver=production (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关闭。

操作方法

s = requests.session()
  s.keep_alive = False
错误"Max retries exceeded with url"表示在尝试多次连接一个URL后,仍然无法建立成功的连接。这种错误可能有多种原因。一种可能是传递给request.get()方法的URL不正确或不完整。另一种可能是受到API的速率限制,超过了允许的连接次数。还可能是requests模块无法验证目标网站的SSL证书。 要解决这个错误,首先要确保在调用request.get()时传递正确且完整的URL。此外,还要确保不受API的速率限制,可以通过调整请求的频率来避免超过限制。如果requests模块无法验证目标网站的SSL证书,可以尝试使用其他方法来验证证书或绕过验证。最后,还可以检查网络连接是否正常,确保可以访问互联网。 如果出现类似"ConnectionError: HTTPConnectionPool(host='xxx.xx.xxx.xxx', port=xxxx): Max retries exceeded with url: /api/v1/login/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7c98a867d0>: Failed to establish a new connection: [Errno 111] Connection refused',))"的错误提示,说明请求过于频繁。一种解决方法是使用sleep方法来减缓请求的频率。另一种解决方法是关闭多余的连接,可以使用以下代码来关闭多余的连接: s = requests.session() s.keep_alive = False 综上所述,当出现"Max retries exceeded with url"错误时,可以通过确保传递正确的URL、避免API速率限制、验证SSL证书、检查网络连接等方法来解决问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python 中错误 ConnectionError: Max retries exceeded with url](https://blog.csdn.net/fengqianlang/article/details/129368257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [解决“Max retries exceeded with url”问题](https://blog.csdn.net/buki26/article/details/85004675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值