0

I just started learning Python and I followed this tutorial to download a picture from a website. It is a very simple code and I get an error as shown below. Does anybody know why I get this error? Something this simple is driving me insane.

I am using PyCharm 4.5.3 and have Python 3.4.

My code:

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")

My error

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
JC203
  • 384
  • 7
  • 18

1 Answers1

0

This error generally occurs when a user is behind a firewall. Most of the common solutions to this problem can be found here. Let us know if none of these solve your problem!

Things I'd try first:

  1. Disabling any active firewalls and try a different internet source if you're at your employer or a wifi hotspot.
  2. If you're using a proxy, you can handle that in urllib, see here
  3. If it still persists, maybe try the second answer from that question, setting the system variable.
  4. Possibly try to recreate your code in a clean environment.

I was able to download the image in PyCharm using your above code without problems, leading me to think you have a firewall issue. Hope this helps!

Community
  • 1
  • 1
J. Clark
  • 18
  • 3
  • Thank you very much. I am behind an employer firewall. I will try some of these options. – JC203 Jul 13 '15 at 20:36