4
votes

I am really new to programming and currently learning python on Youtube ('The New Boston - Python 3' ) Was trying to download an image from internet based on the code show in the video but an error pop up. Here is the code :

import random
import urllib.request

def download_web_image(url):
    name = random.randrange(1,1000)
    full_name = str(name) + '.gif'  #str convert number to word
    urllib.request.urlretrieve(url, full_name)

download_web_image ('https://images.freeimages.com/images/large-previews/ed3/a-stormy-paradise-1-1563744.jpg')

And the error:

 Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1392, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/isaactai/PycharmProjects/isaacfirst/IMAGEDOWNLOAD.py", line 10, in 
    download_web_image ('https://images.freeimages.com/images/large-previews/ed3/a-stormy-paradise-1-1563744.jpg')
  File "/Users/isaactai/PycharmProjects/isaacfirst/IMAGEDOWNLOAD.py", line 8, in download_web_image
    urllib.request.urlretrieve(url, full_name)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: 

Process finished with exit code 1

I am using PyCharm 2018.3 version

Please help me, thank you

1

1 Answers

0
votes

Go to the folder where Python is installed. It should have a name like Python 3.x with x being whatever version of python you installed. Now double click on 'Install Certificates.command'. Had this error before and someone on stack helped me fix it also.

The path for me was as follows: C:\Python33\Tools\Scripts

If that doesn't work here is another workaround using the ssl package:

pip install ssl

Do this before running the code. Then add this to your code.

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

Essentially what this does is make your request "secure" so the HTTPS site will actually accept the request coming from python. You should always do this before trying to access a site with the https prefix.