进行requests库学习的时候,报Max retries exceeded with url错误,经过网上查询可能fiddler开的连接数太多了,简单来说就是目标不可达,链接失效可用try尝试截取错误信息。

http连接太多没有关闭导致的

解决办法:

1、增加重试连接次数

requests.adapters.DEFAULT_RETRIES = 7

2、关闭多余的连接

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

s = requests.session()
s.keep_alive = False

1、在请求之间加入随机等待时间。
2、每次请求之后执行response.close
3、随机使用不同的agent进行访问。
4、使用代理IP。

关闭requests多余的连接

# import requests module 
import requests 
  
# Making a put request 
response = requests.get('https://api.github.com') 
  
# print response 
print(response) 
  
# closing the connection 
response.close() 
  
# Check if this gets execeuted 
print("Connection Closed")
最后修改:2022 年 05 月 10 日
如果觉得我的文章对你有用,请随意赞赏