獲取urllib2.urlopen失敗時的錯誤頁面

錯誤方法:

import urllib2
req = urllib2.Request('http://127.0.0.1/longerrorpage')
try:
    response=urllib2.urlopen(req)
except Exception,e:
    print e, response.read()

HTTP Error 404: Not Found
正確方法:

import urllib2
req = urllib2.Request('http://127.0.0.1/longerrorpage')
try:
    response=urllib2.urlopen(req)
except urllib2.HTTPError,e:
    print e.code
    print e.reason
    print e.geturl()
    print e.read()
404
Not Found
http://127.0.0.1/longerrorpage
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /longerrorpage was not found on this server.</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at 127.0.0.1 Port 80</address>
</body></html>


參考:http://stackoverflow.com/questions/2233687/overriding-urllib2-httperror-and-reading-response-html-anyway


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章