web-dev-qa-db-ja.com

requests.exceptions.HTTPErrorからHTTPエラーコードを取得する

私はこのような例外をキャッチしています、

def get_url_fp(image_url, request_kwargs=None):
    response = requests.get(some_url, **request_kwargs)
    response.raise_for_status()
    return response.raw


try:
   a = "http://example.com"
   fp = get_url_fp(a)

except HTTPError as e:
    # Need to check its an 404, 503, 500, 403 etc.
36
Shiplu Mokaddim

HTTPErrorにはResponseオブジェクトが含まれます。

def get_url_fp(image_url, request_kwargs=None):
    response = requests.get(some_url, **request_kwargs)
    response.raise_for_status()
    return response.raw


try:
    a = "http://example.com"
    fp = get_url_fp(a)

except HTTPError as e:
    # Need to check its an 404, 503, 500, 403 etc.
    status_code = e.response.status_code
68
Lukasa