tags:

views:

219

answers:

4
+1  Q: 

Downloading Image

Hi all,

I used urllib2.build_opener() to download an image from a corresponding url.But for a particular url i am getting error.When i checked that url i came to know that there is no image.How can i check whether there is image or not.This is my code

opener1 = urllib2.build_opener()
page1=opener1.open(orginal)
my_picture=page1.read()

The error i got is

  File "suitcase.py", line 120, in <module>
    get_suitcase()
  File "suitcase.py", line 96, in get_suitcase
    page1=opener1.open(orginal)
  File "D:\Program Files\Python\lib\urllib2.py", line 395, in open
    response = meth(req, response)
  File "D:\Program Files\Python\lib\urllib2.py", line 508, in http_response
    'http', request, response, code, msg, hdrs)
  File "D:\Program Files\Python\lib\urllib2.py", line 433, in error
    return self._call_chain(*args)
  File "D:\Program Files\Python\lib\urllib2.py", line 367, in _call_chain
    result = func(*args)
  File "D:\Program Files\Python\lib\urllib2.py", line 516, in http_error_default

    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

How to check image is there and proceed with saving that image

regards

arun

+1  A: 

I don't understand. Why just not catch the error with the try and except keywords?

monoceres
A: 

By checking the code attribute of the exception.

Ignacio Vazquez-Abrams
+1  A: 

as others have suggested catch the exception and check for code e.g.

import urllib2

opener1 = urllib2.build_opener()
try:
    page1=opener1.open("http://www.google.com/nosuchimage")
    my_picture=page1.read()
except urllib2.HTTPError,e:
    if e.code == 404:
        print "no such image"
    else:
        print "error",e
except urllib2.URLError,e:
    print "URLError",e
Anurag Uniyal
Thanks....When i tested with the above code it works..but new error shown like this File "/usr/lib/python2.5/httplib.py", line 860, in endheaders self._send_output() File "/usr/lib/python2.5/httplib.py", line 732, in _send_output self.send(msg) File "/usr/lib/python2.5/httplib.py", line 699, in send self.connect() File "/usr/lib/python2.5/httplib.py", line 683, in connect raise socket.error, msg IOError: [Errno socket error] (110, 'Connection timed out') i tried wid the Except IOError but it is not fixed
are you ure you are using urllib2 ? becuase for timeout it may raise urllib2.URLError ? I have added that too
Anurag Uniyal
A: 
try:
    page1=opener1.open(orginal)
except HTTPError, e:
    if e.code == 404: # Only one of the many possible errors...
        print "Resource does not exist"
    raise

my_picture=page1.read() 

see also urllib2 - the missing manual

extraneon
catch? python can't catch!
Anurag Uniyal
@Anurag, You're right. Too much java lately. Fixed it :)
extraneon
Thanks....When i tested with the above code it works..but new error shown like thisFile "/usr/lib/python2.5/httplib.py", line 860, in endheaders self._send_output() File "/usr/lib/python2.5/httplib.py", line 732, in _send_output self.send(msg) File "/usr/lib/python2.5/httplib.py", line 699, in send self.connect() File "/usr/lib/python2.5/httplib.py", line 683, in connect raise socket.error, msgIOError: [Errno socket error] (110, 'Connection timed out') i tried wid the Except IOError but it is not fixed
@arung A socket error is very different from a http error. First check with a browser wether "original" is available and downloadable. You should see the image. If that works, you might use the wrong protocol (http / https) or the wrong port (default for http is 80); again, the URL specified in the browser should be retrievable by urllib2.
extraneon