[684]requests時報錯requests.exceptions.SSLError: HTTPSConnectionPool

  • 2020 年 1 月 13 日
  • 筆記

報錯資訊

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

分析:由於報錯SSL證書驗證失敗,所以這次的訪問應該是https協議.但是我們明明使用的是http,所以,猜測訪問該網站後,被重定向到了https

解決:關閉證書驗證.因為,如果不關閉,請求總是失敗,不能獲取到重定向的資訊.

安裝一下幾個requests依賴包,然後設置, verify=False

pip install cryptography  pip install pyOpenSSL  pip install certifi
>>> response = requests.get('http://www.baidu.com/', headers = header, verify=False)  D:pythonlibsite-packagesurllib3connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings  InsecureRequestWarning)  >>> response.history  [<Response [302]>]  >>> response.url  u'https://www.baidu.com/'

結果出現警告,Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)

想去掉紅框內的內容還需要添加如下程式碼

requests.packages.urllib3.disable_warnings()

參考:https://blog.csdn.net/win_turn/article/details/77142100 https://www.cnblogs.com/hum0ro/p/9536033.html https://blog.csdn.net/jss19940414/article/details/89886043