1

So I'm doing some web-scraping, and I'm trying to download an image from an URL.

Here's my code:

import urllib
from urllib import request

urllib.request.urlretrieve(url, 'image.jpg')

I get 2 errors when I run this code:

  • ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:645)

  • urllib.error.URLError: urlopen error [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:645)>

Tried to look up answers on Google, nothing helped.

Thanks

vimuth
  • 5,064
  • 33
  • 79
  • 116
Michael
  • 503
  • 8
  • 18
  • See this similar question https://stackoverflow.com/questions/33770129/how-do-i-disable-the-ssl-check-in-python-3-x – kilojoules Apr 03 '18 at 20:36

1 Answers1

1

You can use this

import ssl
import urllib.request
url = 
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

with urllib.request.urlopen(url, context=ctx) as u, \
        open('image.jpg', 'wb') as f:
    f.write(u.read())

UPDATE

If above is not working for you, you can use

from urllib.request import Request, urlopen
url = 

req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
with open('image.jpg', 'wb') as f:
    f.write(webpage)
Michael
  • 503
  • 8
  • 18
Agam Banga
  • 2,708
  • 1
  • 11
  • 18