Download and save a picture - urllib

mattia gervaz at gmail.com
Thu Sep 10 14:59:46 EDT 2009


You were right, the problem was with the print function, using a normal 
write everythong works fine.

Il Thu, 10 Sep 2009 18:56:07 +0200, Diez B. Roggisch ha scritto:

> 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