requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘****, port=80): Max retries exceeded w

5 篇文章 0 订阅
3 篇文章 0 订阅

python requests.get爬虫时,跑几个数据后,报错:requests.exceptions.ConnectionError: HTTPConnectionPool(host='****, port=80): Max retries exceeded with url: /beijing/file/4023E7D190674D26934AED5F4306DBC0/B76E1E3D9DF842D6ACBC63978C3A89FE/977FE5585248441E86067EFBF097E587/zz/ew3.jpg (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbc166b6490>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

具体如下:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 156, in _new_conn
    conn = connection.create_connection(
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.8/http/client.py", line 1240, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1286, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1235, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1006, in _send_output
    self.send(msg)
  File "/usr/lib/python3.8/http/client.py", line 946, in send
    self.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 184, in connect
    conn = self._new_conn()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 168, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fbc166b6490>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen
    retries = retries.increment(
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='****', port=80): Max retries exceeded with url: /beijing/file/4023E7D190674D26934AED5F4306DBC0/B76E1E3D9DF842D6ACBC63978C3A89FE/977FE5585248441E86067EFBF097E587/zz/ew3.jpg (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbc166b6490>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/data/xll/code/dzy/ddjs/parsebj.py", line 143, in <module>
    r = s.get(url=url, headers=headers, timeout=60)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 543, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='****, port=80): Max retries exceeded with url: /beijing/file/4023E7D190674D26934AED5F4306DBC0/B76E1E3D9DF842D6ACBC63978C3A89FE/977FE5585248441E86067EFBF097E587/zz/ew3.jpg (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbc166b6490>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

在网上找到一些办法:参考https://blog.csdn.net/a1007720052/article/details/83383220

在请求头中加:'Connection': 'close'
设置请求间隔:time.sleep(5)
设置session:

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

都没有解决这个问题,后来发现原因处在:socket.gaierror: [Errno -3] Temporary failure in name resolution

参考:https://stackoverflow.com/questions/40238610/what-is-the-meaning-of-gaierror-errno-3-temporary-failure-in-name-resolutio

域名解析失败,修改域名为ip地址

r = requests.get(url=url.replace("<域名>", "<IP>"))

例如:

r = requests.get(url=url.replace("aaa.bb.com", "123.12.13.14"))

总结:改域名为ip

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8097) 是一个网络连接错误的异常。它表示在与特定的主机和端口建立连接时出现问题。具体错误信息是"Max retries exceeded with url: /env/main (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000019B93886588>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))"。这个错误通常是因为无法连接到目标主机所导致的。可能的原因包括目标主机处于离线状态,网络连接不稳定,或目标主机主动拒绝了连接请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [requests.exceptions.ConnectionError:HTTPSConnectionPool(host](https://blog.csdn.net/a6864657/article/details/94381948)[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* [visdom安装及启动/requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘localhost‘, port=...](https://blog.csdn.net/qq_42514371/article/details/127838311)[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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值