我使用urllib2.build_opener()從相應的url下載圖片。但是對於特定的網址,我收到錯誤消息。當我查看該網址時,我看到沒有圖像。我如何檢查是否有圖像?這是我的代碼:正在下載圖片
opener1 = urllib2.build_opener()
page1=opener1.open(orginal)
my_picture=page1.read()
我得到的錯誤是
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
如何檢查的圖像是存在的,與儲蓄的形象繼續嗎?
由於....當我與上面的代碼測試它works..but這樣文件「/usr/lib/python2.5/httplib所示新的錯誤.py「,第860行,在endheaders中self._send_output()文件」/usr/lib/python2.5/httplib.py「,第732行,在_send_output self.send(msg)文件中」/ usr/lib/python2。 5/httplib.py「,第699行,發送self.connect()文件」/usr/lib/python2.5/httplib.py「,第683行,在connect raise socket.error,msg IOError:[Errno socket error ](110,'連接超時')我嘗試了wid Except IOError,但它不是固定的 – user244470 2010-02-22 10:34:05
你是否正在使用url lib2?因超時而可能會引發urllib2.URLError?我還補充說, – 2010-02-22 11:25:27