python爬虫 requests异常requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded

python爬虫 requests异常requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded,第1张

概述python爬虫 requests异常requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded

错误提示:

requests.exceptions.ConnectionError: httpSConnectionPool(host='baike.baIDu.com', port=443): Max retrIEs exceeded with url: https://baike.baIDu.com/item/%E5%88%98%E5%BE%B7%E5%8D%8E/114923 (Caused by NewConnectionError('<urllib3.connection.VerifIEdhttpSConnection object at 0x7fb51433af98>: Failed to establish a new connection: [Errno -2] name or service not kNown',))

经过一番查询,发现该错误是因为如下:

http的连接数超过最大限制,默认的情况下连接是Keep-alive的,所以这就导致了服务器保持了太多连接而不能再新建连接。

ip被封

程序请求速度过快。

解决办法如下:

第一种方法

try:    page1 = requests.get(ap)except requests.exceptions.ConnectionError:    r.status_code = "Connection refused"

第二种方法:

request的连接数过多而导致Max retrIEs exceeded

在header中不使用持久连接

'Connection': 'close'

requests.adapters.DEFAulT_RETRIES = 5

第三种方法:

针对请求请求速度过快导致程序报错。

解决方法可以参考以下例子:

import timewhile 1:    try:        page = requests.get(url)    except:        print("Connection refused by the server..")        print("Let me sleep for 5 seconds")        print("ZZzzzz...")        time.sleep(5)        print("Was a nice sleep, Now let me continue...")        continue


总结

以上是内存溢出为你收集整理的python爬虫 requests异常requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded全部内容,希望文章能够帮你解决python爬虫 requests异常requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1198348.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存