所以,我的代碼只有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>
這是我都試過了。
- 我搜索返回的錯誤「的urlopen錯誤[錯誤11002]」在谷歌,尤其是對計算器,沒有什麼有用的返回(事實上,有沒有這個錯誤11002多有人問)。
- 所以然後我嘗試用另一個網站「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>
問題解決了。而且我不得不提及,我在中國,而我試圖連接的網站被臭名昭着的GFW封鎖。我打開燈籠(代理軟件之一來繞過審查。)並添加環境變量,如你所建議的。它的工作原理雖然有點落後。認爲這可能會幫助像我這樣的中國人。 –