requests-html HTTPSConnectionPool

发布时间:2019-09-27 16:29:27编辑:Run阅读(4170)

    requests-html 捕获访问超时异常

    from requests_html import HTMLSession
    import requests
    
    session = HTMLSession()
    url = 'https://www.zaful.com/men-e_118/?featuring=454613605,454613605,454620801,454622001,454615201,454622901"\n'
    try:
        r = session.get(url=url, timeout=0.1)
        if r.status_code == 200:
            title_ret = r.html.xpath("/html/head/title/text() | //title/text()")
            if title_ret:
                title_ret = title_ret[0].strip().replace("\n", "")
            print(title_ret)
        else:
            print(r.status_code)
    except requests.exceptions.ReadTimeout as read_timeout:
        print(read_timeout)
    except requests.exceptions.ConnectTimeout as connect_timeout:
        print(connect_timeout)
    except Exception as e:
        print(e)
    finally:
        session.close()

    执行结果

    HTTPSConnectionPool(host='hk.zaful.com', port=443): Read timed out. (read timeout=0.1)

关键字