Dyllian
Dyllian

Reputation: 223

How can I download this image in Python, without seeing "figure is damaged"?

I want to dowload an image from http://img13.360buyimg.com/n2/15153/dfa49f49-0e2d-422f-81f9-23a086a7e957.jpg.

But using the following Python code, I get "Figure is damaged". Why? This is the python code I used to achieve this:

req = urllib2.Request(img_url,headers=headers)`

Upvotes: 0

Views: 706

Answers (2)

Nafiul Islam
Nafiul Islam

Reputation: 82470

You can achieve the same thing through much easier means if your use requests. If you're new to the whole thing, then requests will be an easy way to do these kinds of things. Here is a link to requests. You can get what you want through a simple requests' get function, so, something like requests.get(url).

And you can save it like this:

with open("somefile.jpg","wb") as f:
     f.write(file.content)

All the extra documentation that you might need is already on the requests website. In order to install requests, you are going to need pip and then use pip install requests to install requests.

Start using requests now because it makes working with get and post as well as other kinds of interaction much easier.

Upvotes: 2

Saurabh7
Saurabh7

Reputation: 720

You can retrieve the file using urllib.urlretrieve,

urllib.urlretrieve("http://img13.360buyimg.com/n2/15153/dfa49f49-0e2d-422f-81f9-23a086a7e957.jpg" , "filename.jpg")

more info in python docs.

Upvotes: 0

Related Questions