我刚开始学习Python,我按照this教程从网站下载图片。这是一个非常简单的代码,我得到一个错误,如下所示。有人知道我为什么会收到这个错误吗?这么简单的事情让我疯狂。
我正在使用PyCharm 4.5.3并使用Python 3.4。
我的代码:
import random
import urllib.request
def download_web_image(url):
name = random.randrange(1, 1000)
full_name = str(name) + ".jpg"
urllib.request.urlretrieve(url, full_name)
download_web_image("https://upload.wikimedia.org/wikipedia/en/5/51/Name.jpeg")
我的错误
C:\Python34\python.exe D:/Users/212409097/PycharmProjects/HTTP_Server/Example.py
Traceback (most recent call last):
File "C:\Python34\lib\urllib\request.py", line 1182, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Python34\lib\http\client.py", line 1088, in request
self._send_request(method, url, body, headers)
File "C:\Python34\lib\http\client.py", line 1126, in _send_request
self.endheaders(body)
File "C:\Python34\lib\http\client.py", line 1084, in endheaders
self._send_output(message_body)
File "C:\Python34\lib\http\client.py", line 922, in _send_output
self.send(msg)
File "C:\Python34\lib\http\client.py", line 857, in send
self.connect()
File "C:\Python34\lib\http\client.py", line 1223, in connect
super().connect()
File "C:\Python34\lib\http\client.py", line 834, in connect
self.timeout, self.source_address)
File "C:\Python34\lib\socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Users/212409097/PycharmProjects/HTTP_Server/Example.py", line 10, in <module>
download_web_image("https://upload.wikimedia.org/wikipedia/en/5/51/Name.jpeg")
File "D:/Users/212409097/PycharmProjects/HTTP_Server/Example.py", line 8, in download_web_image
urllib.request.urlretrieve(url, full_name)
File "C:\Python34\lib\urllib\request.py", line 186, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 463, in open
response = self._open(req, data)
File "C:\Python34\lib\urllib\request.py", line 481, in _open
'_open', req)
File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 1225, in https_open
context=self._context, check_hostname=self._check_hostname)
File "C:\Python34\lib\urllib\request.py", line 1184, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
Process finished with exit code 1
答案 0 :(得分:0)
当用户位于防火墙后面时,通常会发生此错误。可以找到解决此问题的大多数常见解决方案here。如果这些都没有解决您的问题,请告诉我们!
我能够使用上面的代码在PyCharm中下载图像而没有任何问题,这让我觉得你有防火墙问题。希望这有帮助!