requests包的异常处理

可以使用raise_for_status()来获取所有错误,并且在except语句中使用 requests.RequestException来得到错误原因:

def get_bs_obj(link):
    try:
        response = requests.get(link, headers=headers, timeout=10)
        response.raise_for_status()
        bs_obj = bs(response.text)
        return bs_obj
    except requests.RequestException as e:
        print(e)
        return None

例如会产生报错:

HTTPConnectionPool(host='synthezise.christuniversity.in', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000021B9CB8A0F0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

HTTPConnectionPool(host='icu2018cls.umk.edu.my', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000021B9CB8ACF8>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

HTTPSConnectionPool(host='icsah.eu', port=443): Max retries exceeded with url: /events (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))

HTTPSConnectionPool(host='icsah.eu', port=443): Max retries exceeded with url: /events (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))