Re: Download and save a picture - urllib

2009-09-24 Thread Piet van Oostrum
 MRAB pyt...@mrabarnett.plus.com (M) wrote:

M mattia wrote:
 You were right, the problem was with the print function, using a normal
 write everythong works fine.
 
M You should open the output file as binary (it doesn't matter on
M Linux/Unix, but is a good idea anyway for portability).

It probably DOES matter also on Linux/Unix, as the OP is using Python3.
-- 
Piet van Oostrum p...@cs.uu.nl
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Download and save a picture - urllib

2009-09-11 Thread mattia
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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Download and save a picture - urllib

2009-09-10 Thread Diez B. Roggisch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Download and save a picture - urllib

2009-09-10 Thread mattia
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Download and save a picture - urllib

2009-09-10 Thread MRAB

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



You should open the output file as binary (it doesn't matter on
Linux/Unix, but is a good idea anyway for portability).


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.



--
http://mail.python.org/mailman/listinfo/python-list