Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urllib.request.urlretrieve ERROR trying to download jpeg in Python

I am trying to download a .jpg file, using urllib.request.urlretrieve(url, filename) in Python 3.5.2. The url is http://dm.victoriassecret.com/product/404x539/V603923_CROP1.jpg . The following error raises: http.client.RemoteDisconnected: Remote end closed connection without response.

I also have a problem when trying the same with this url = http://lp2.hm.com/hmprod?set=source[/model/2017/9AS 0505882 002 00 0034.jpg],type[STILLLIFE_FRONT]&hmver=0&call=url[file:/product/style] .

In that case the following error raises: raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 505: HTTP Version not supported

Does anybody know what is the problem with these urls and how could I fix it? Sharing your knowledge with me, would be nice.

like image 687
dimosbele Avatar asked Nov 02 '25 07:11

dimosbele


1 Answers

The remote isn't responding because you're lacking headers in your request. Furthermore, I suggest you use the requests module (install it via pip install requests), as it's way better and faster than urllib:

import requests
headers = headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Cafari/537.36'}

pic = requests.get('http://dm.victoriassecret.com/product/404x539/V603923_CROP1.jpg', headers=headers)

with open('beautiful.jpg', 'wb') as photo:
    photo.write(pic.content)

Now open your working directory and you'll find the image residing there.

This will also work with your other link.

like image 179
Sam Chats Avatar answered Nov 03 '25 22:11

Sam Chats



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!