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

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐