【问题标题】:Grabbing Image off Website Error从网站错误中抓取图像
【发布时间】:2015-07-10 20:52:30
【问题描述】:

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

【问题讨论】:

    标签: pycharm urllib python-3.4


    【解决方案1】:

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

    我首先要尝试的事情:

    1. 如果您在您的雇主或 wifi 热点,请禁用所有活动的防火墙并尝试不同的 Internet 资源。
    2. 如果您使用代理,您可以在 urllib 中处理,请参阅here
    3. 如果它仍然存在,不妨试试该问题的第二个答案,setting the system variable
    4. 可能尝试在干净的环境中重新创建代码。

    我能够使用您上面的代码在 PyCharm 中毫无问题地下载图像,这让我认为您遇到了防火墙问题。希望这会有所帮助!

    【讨论】:

    • 非常感谢。我在雇主防火墙后面。我会尝试其中一些选项。
    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多