从网站错误抓取图像

Grabbing Image off Website Error

我刚开始学习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

当用户位于防火墙后面时,通常会发生此错误。可以找到此问题的大多数常见解决方案 here。如果 none 这些解决了您的问题,请告诉我们!

我首先尝试的事情:

  1. 禁用任何活动的防火墙并尝试不同的互联网来源,如果你在你的雇主或 wifi 热点。
  2. 如果您使用的是代理,则可以在 urllib 中进行处理,请参阅 here
  3. 如果它仍然存在,也许可以尝试该问题的第二个答案,setting the system variable
  4. 可能会尝试在干净的环境中重新创建您的代码。

我可以使用您的上述代码毫无问题地下载 PyCharm 中的图像,这使我认为您遇到了防火墙问题。希望这对您有所帮助!