Python urllib2: Reading content body even during HTTPError exception?

import urllib2
try:
    request = urllib2.Request('http://www.somesite.com')
    response = urllib2.urlopen(req)
except urllib2.HTTPError as e:
    error_message = e.read()
    print error_message

You can treat the error as a response.

http://www.voidspace.org.uk/python/articles/urllib2.shtml#httperror

When an error is raised the server responds by returning an HTTP error code and an error page. You can use the HTTPError instance as a response on the page returned. This means that as well as the code attribute, it also has read, geturl, and info, methods.

Tags:

Python