从网站抓取图像错误



我刚开始学习Python,我按照这个教程从一个网站下载了一张图片。这是一个非常简单的代码,我得到一个错误,如下所示。有人知道为什么会出现这个错误吗?这么简单的事情就快把我逼疯了。

我正在使用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:Python34python.exe D:/Users/212409097/PycharmProjects/HTTP_Server/Example.py
Traceback (most recent call last):
  File "C:Python34liburllibrequest.py", line 1182, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:Python34libhttpclient.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "C:Python34libhttpclient.py", line 1126, in _send_request
    self.endheaders(body)
  File "C:Python34libhttpclient.py", line 1084, in endheaders
    self._send_output(message_body)
  File "C:Python34libhttpclient.py", line 922, in _send_output
    self.send(msg)
  File "C:Python34libhttpclient.py", line 857, in send
    self.connect()
  File "C:Python34libhttpclient.py", line 1223, in connect
    super().connect()
  File "C:Python34libhttpclient.py", line 834, in connect
    self.timeout, self.source_address)
  File "C:Python34libsocket.py", line 494, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:Python34libsocket.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:Python34liburllibrequest.py", line 186, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "C:Python34liburllibrequest.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "C:Python34liburllibrequest.py", line 463, in open
    response = self._open(req, data)
  File "C:Python34liburllibrequest.py", line 481, in _open
    '_open', req)
  File "C:Python34liburllibrequest.py", line 441, in _call_chain
    result = func(*args)
  File "C:Python34liburllibrequest.py", line 1225, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:Python34liburllibrequest.py", line 1184, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
Process finished with exit code 1

此错误通常发生在用户处于防火墙后面时。这个问题的大多数常见解决方案都可以在这里找到。如果这些都不能解决你的问题,请告诉我们!

我想先尝试的事情:

  1. 禁用任何活跃的防火墙,如果你在你的雇主或wifi热点尝试不同的互联网来源。
  2. 如果你使用代理,你可以在urllib中处理,见这里
  3. 如果仍然存在,也许可以尝试第二个答案,设置系统变量。
  4. 可能尝试在一个干净的环境中重新创建你的代码。

我能够使用您的上述代码在PyCharm中下载图像而没有问题,这使我认为您有防火墙问题。希望这对你有帮助!

相关内容

  • 没有找到相关文章

最新更新