2016-03-07 2275 views
1

所以,我的代碼只有4行。我試圖連接到一個網站,然後我試圖做的是無關緊要的,因爲沒有其他代碼就會出現錯誤。urllib.error.URLError:<urlopen錯誤[Errno 11002] getaddrinfo失敗>?

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. 我搜索返回的錯誤「的urlopen錯誤[錯誤11002]」在谷歌,尤其是對計算器,沒有什麼有用的返回(事實上,有沒有這個錯誤11002多有人問)。
  2. 所以然後我嘗試用另一個網站「http://www.pythonlearn.com/code/urllinks.py」替換網站參數(即「http://python-data.dr-chuck.net/known_by_Fikret.html」),並且它工作得很好,沒有錯誤出現。這個特定的網站本身,這個網站有些動態,我的意思是它的內容會改變,變成另一個完全不同的東西,但我沒有更多知識不僅僅是爲了描述我所看到的。

和錯誤的時間更長,完整版:

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> 

回答

2

這意味着要麼你的DNS系統不能正常工作,或者你必須使用一個代理在網絡上並沒有定義正確。

如果您需要使用代理服務器,請將環境變量HTTP_PROXY(和可選的HTTPS_PROXY)設置爲網絡的正確配置。格式爲http://proxy.example.com:80;如果你的代理需要一個用戶名和密碼,你應該通過它,像這樣:http://username:[email protected]:80

對於DNS問題,請嘗試從命令行查找域。打開命令提示符並鍵入nslookup python-data.dr-chuck.net並查看它是否返回給您一個IP地址。

+0

問題解決了。而且我不得不提及,我在中國,而我試圖連接的網站被臭名昭着的GFW封鎖。我打開燈籠(代理軟件之一來繞過審查。)並添加環境變量,如你所建議的。它的工作原理雖然有點落後。認爲這可能會幫助像我這樣的中國人。 –

2

這是DNS的問題;顯然你的python程序無法解析你提供的URL的主機名。也許DNS在其運行的主機上配置錯誤?

此外,我可以推薦使用請求庫嗎?這是一個更好,更容易使用的模塊來做網絡請求。你會在這裏找到它:https://pypi.python.org/pypi/requests

相關問題