python - 使用请求模块时无法建立新连接:[Errno 11001] getaddrinfo failed'))
问题描述
最初尝试使用一个 url 并编译 requests.get(url) 它工作。但现在我想检查一个 URL 列表。
所以我做了一个 For 循环,每次都调用请求。但这并没有遵守,也不明白是什么问题。我在列表http://microsoft.com和http://google.com中使用了两个 URL 。我尝试通过提供 https 但收到相同的错误。
import requests
def main():
f=open("list.txt","r")
print(f)
f1= f.readlines()
for x in f1:
print (x)
r= requests.get(x)
print(r.status_code)
if __name__ == "__main__":
main()
以下是编译程序时收到的错误
<_io.TextIOWrapper name='list.txt' mode='r' encoding='cp1252'>
http://microsoft.com
Traceback (most recent call last):
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
chunked=chunked,
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1244, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1290, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1239, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 966, in send
self.connect()
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connection.py", line 187, in connect
conn = self._new_conn()
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x03856FD0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool.py", line 725, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='microsoft.com%0a', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x03856FD0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tool.py", line 13, in <module>
main()
File "tool.py", line 8, in main
r= requests.get(x)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\lycan333\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='microsoft.com%0a', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x03856FD0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
解决方案
推荐阅读
- android - 在android studio中添加数据绑定后编译问题
- reactjs - 我可以将 Recharts 轴域最大值设置为低于 dataMax 吗?
- c# - StreamWriter 和 StreamReader 以及数组问题
- java - 如何从 Java 中的 Unirest 响应中获取原始 JSON 文本
- python - 3DS Max Python 脚本
- javascript - 试图有一个窗口,其中七人行可见而其余部分被隐藏 - 图像是 div 标签中的 spritesheet.png
- c++ - 在随机位置生成固定数量的 1
- sql-server - 使用所有步骤的自动参数执行存储过程 iIn SQL Server 代理
- javascript - Knex JS“或”在 .where() 内?
- json - JSONDecodeError:期望值:使用 Json 的第 1 行第 1 列