1

I tried to download an image from the web using Python 3.7. But I got some error in my code and I cannot understand what is wrong in my code and how to recover it. I use PyCharm 3.4 and MacOS X:

My Code:

import urllib.request
urllib.request.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

Error

urllib.error.URLError: <urlopen error [Errno 65] No route to host>
sigma
  • 227
  • 3
  • 13

2 Answers2

1

Your approach is correct. However, the link itself is dead; hence the error.

Simply use urllib.request.urlretrieve(url=link, filename=output), your approach is correct. If the url is an image, you download an image. If the url is an HTML file, you download a HTML file.

Your error urllib.error.URLError: <urlopen error [Errno 65] No route to host> is because your link is broken. The urlretrieve only works for non-broken links. Additionally, urlretrieve is considered to belong in the legacy interface.

Unfortunately, there is nothing you can do to fix the URL "http://www.digimouth.com/news/media/2011/09/google-logo.jpg" and it also appears to be suspicious now.

0

The code you provided works for a different picture, for example:

import urllib.request
urllib.request.urlretrieve("https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwi7nsiIqqXgAhUnuqQKHY6uDa4QjRx6BAgBEAU&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGiraffe&psig=AOvVaw1g8lkjuT8Ly2FxVhGp1vp6&ust=1549481373274429", "giraffe.jpg")

The problem is with your link, as http://www.digimouth.com/news/media/2011/09/google-logo.jpg seems to be dead. Even wget http://www.digimouth.com/news/media/2011/09/google-logo.jpg does not work in terminal and Chrome cannot open that link properly. So I suggest to choose a different image.

For SSL error, see: https://stackoverflow.com/a/28052583/8565438

zabop
  • 6,750
  • 3
  • 39
  • 84
  • ,,,I copy and paste your link to my code but this time following error occured: ```urllib.error.URLError: ``` – sigma Feb 06 '19 at 03:44
  • No still the code cannot download images from the given website: I added the followings: ```import ssl``` ```context = ssl._create_unverified_context()``` – sigma Feb 06 '19 at 06:51