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.
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.
|
|
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()
.":
|
|
Alternatively, you can hide warnings for only unsafe requests:
|
|
References:
Requests package - SSL Cert Verification
urllib3 package - TLS Warnings
stackoverflow
真正能讓你倒下的,不是對手,而是你絕望的內心。
🔻 如果覺得喜歡,歡迎在下方獎勵我 5 個讚~