Download and save a picture - urllib

mattia gervaz at gmail.com
Thu Sep 10 18:35:29 CEST 2009


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?



More information about the Python-list mailing list