Python urllib2: Reading content body even during HTTPError exception?

前端 未结 3 1595
天命终不由人
天命终不由人 2021-01-31 13:38

I\'m using urllib2 to fetch a a page via HTTP. Sometimes the resource throws a HTTP error 400 (Bad Request) when my request contains an error. However, that response also cont

3条回答
  •  野的像风
    2021-01-31 14:00

    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.

提交回复
热议问题