HTTPConnectionPool(host='xx.xx.xx.xx', port=xx): Max retries exceeded with url:(Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x0000015A25025EB8>...))

HTTPConnectionPool(host='xx.xx.xx.xx', port=xx): Max retries exceeded with url:(Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x0000015A25025EB8>, 'Connection to xx.xx.xx.xx timed out. (connect timeout=10)'))

网查说是http连接太多没有关闭导致的

解决办法:

1、增加重试连接次数

requests.adapters.DEFAULT_RETRIES = 5

2、关闭多余的连接

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

s = requests.session()

s.keep_alive = False

网搜的解决方案,我的代码在for循环里不断post url,的确是打开了很多连接没关闭。

不知道是不是这个原因,加上上述3行代码之后,代码跑完了。

除上述改动外,timeout由原来的10改为了30。

还有另外一种说法是在上述3行代码基础上再挂代理,代理设置一下proxies参数即可,不贴代码了。

不过没有尝试,可用的免费代理比较少,效率会非常低,不管有用否,不尝试了。

unfortunately,第二次再跑又报如下错误:

HTTPConnectionPool(host=''xx.xx.xx.xx', port=xx): Max retries exceeded with url: (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000237EE44EF98>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反 应,连接尝试失败。',))

太晚了,先睡了,回头再说吧。

猜你喜欢

转载自www.cnblogs.com/BeyondTechnology/p/9581712.html
xx
今日推荐