This page looks best with JavaScript enabled

Python Requests Error: requests.exceptions.SSLError: certificate verify failed - Unable to Access Web Page

    Issue

    When using the Requests package to fetch a webpage, you might sometimes encounter the following error:

    requests.exceptions.SSLError: HTTPSConnectionPool(host='www.xxx.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),))
    

    The cause is often related to HTTPS SSL certificate issues—such as the certificate being expired, invalid, or having other problems—which leads Requests to fail verification during the request and results in the requests.exceptions.SSLError error.

    Invalid Certificate
    Invalid Certificate

    Solution

    By default, the Requests package has SSL verification enabled. One way to resolve this issue is to disable SSL certificate verification by adding the parameter verify=False to the request.

    1
    
    r = requests.get('https://www.xxx.com', verify=False)
    



    Disabling SSL certificate verification in the Requests package allows you to retrieve the webpage data, but it will also display a warning:

    InsecureRequestWarning: Unverified HTTPS request is being made to host 'www.xxx.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
    

    While this warning does not affect the result, you might find it distracting. To hide this warning message, refer to the following guide (https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings), which states: “if you understand the risks and wish to disable these warnings, you can use disable_warnings().":

    1
    2
    
    import urllib3
    urllib3.disable_warnings()
    

    Alternatively, you can hide warnings for only unsafe requests:

    1
    2
    
    import urllib3
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    



    References:
    Requests package - SSL Cert Verification
    urllib3 package - TLS Warnings
    stackoverflow


    真正能讓你倒下的,不是對手,而是你絕望的內心。


    🔻 如果覺得喜歡,歡迎在下方獎勵我 5 個讚~
    Share on

    Jia
    WRITTEN BY
    Jia
    軟體工程師 - Software Engineer