urllib.error.URLError:<urlopen error[Errno 11002]getaddrinfo failed>

2024-04-27 11:46:02 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我的代码只有4行。我试图连接到一个网站,我试图做的是无关的,因为没有其他代码的错误发生。

import urllib.request
from bs4 import BeautifulSoup 

html=urllib.request.urlopen('http://python-data.dr-chuck.net/known_by_Fikret.html').read()
soup=BeautifulSoup(html,'html.parser')

以及错误(简明扼要地总结了一个错误):

for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11002] getaddrinfo failed
During handling of the above exception, another exception occurred:
urllib.error.URLError: <urlopen error [Errno 11002] getaddrinfo failed>

这是我试过的。

  1. 我在google上搜索了返回的错误“urlopen Error[Errno 11002]”,特别是stackoverflow上,没有返回任何有用的信息(事实上,对这个错误11002没有太多问题)。
  2. 因此,我尝试用另一个网站“http://www.pythonlearn.com/code/urllinks.py”替换urlopen函数中的网站参数(即“http://python-data.dr-chuck.net/known_by_Fikret.html”)。而且效果很好。不会出错。
  3. 所以我想这个错误一定和这个网站本身有关。这个网站有点动态,我的意思是它的内容会改变,变成另一个完全不同的东西。但我所知道的不过是描述我所看到的。

以及错误的更长和完整版本:

Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1240, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1083, in request
self._send_request(method, url, body, headers)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1128, in _send_request
self.endheaders(body)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1079, in endheaders
self._send_output(message_body)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 911, in _send_output
self.send(msg)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 854, in send
self.connect()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 826, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11002] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:/baiduyundownload/Tempo/Active/Python/Python Examples/Fileanalysis11111.py", line 4, in <module>
html=urllib.request.urlopen('http://python-data.dr-chuck.net/known_by_Fikret.html').read()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 465, in open
response = self._open(req, data)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 483, in _open
'_open', req)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1268, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1242, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11002] getaddrinfo failed>

Tags: inpyselfhttprequestliblocalline
2条回答

这是DNS的问题;显然python程序无法解析您提供的URL的主机名。可能DNS在它运行的主机上配置不正确?

另外,我可以推荐使用请求库吗?这是一个更好、更易于使用的模块来处理web请求。你可以在这里找到它:https://pypi.python.org/pypi/requests

这意味着你的DNS系统不能正常工作,或者你必须在你的网络上使用一个代理并且它没有被正确定义。

如果需要使用代理,请将环境变量HTTP_PROXY(以及HTTPS_PROXY)设置为网络的正确配置。格式为http://proxy.example.com:80;如果代理需要用户名和密码,则应将其传入,如下所示:http://username:password@proxy.example.com:80

有关DNS问题,请尝试从命令行查找域。打开命令提示符并键入nslookup python-data.dr-chuck.net,然后查看它是否返回给您一个IP地址。

相关问题 更多 >