Python urllib2: Reading content body even during HTTPError exception?

前端 未结 3 1648
天命终不由人
天命终不由人 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 13:48

    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
    

提交回复
热议问题