Download and save a picture - urllib

Diez B. Roggisch deets at nospam.web.de
Thu Sep 10 12:56:07 EDT 2009


mattia wrote:

> Hi all, in order to download an image. In order to correctly retrieve the
> image I need to set the referer and handle cookies.
> 
> opener = urllib.request.build_opener(urllib.request.HTTPRedirectHandler
> (), urllib.request.HTTPCookieProcessor())
> urllib.request.install_opener(opener)
> req = urllib.request.Request("http://myurl/image.jpg")
> req.add_header("Referer", "http://myulr/referer.jsp")
> r = urllib.request.urlopen(req)
> with open("image.jpg", "w" ) as fd:
>     print(r.read(), file=fd)
> 
> I'm not able to correctly save the image. In fact it seems that it it
> saved in hex format. Any suggestion?

How do you come to the conclusion that it's saved as "hex"? It sure isn't -
either the request fails because the website doesn't allow it due to
missing cookies or similar stuff - or you get the binary data.

But you should be aware that in the interpreter, strings are printed out
with repr() - which will convert non-printable characters to their
hex-representation to prevent encoding/binary-data-on-teriminal-issues.

Diez



More information about the Python-list mailing list