logo
Loading...

Day5, 執行 requests.get 時出現 SSLError - Cupoy

import requeststarget_url = 'https://raw.githubuse...

ml100,ml100-d05

Day5, 執行 requests.get 時出現 SSLError

2020/07/28 上午 11:46
機器學習共學討論版
徐偉峻
觀看數:20
回答數:1
收藏數:0
ml100
ml100-d05


執行到要抓取網頁內容的時候出現下方一連串的SSLError錯誤。

嘗試過 requests.get(target_url, verify=False), 但會出現C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py:847: 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)


這個網址若直接用瀏覽器開啟是正常的。

請問該如何讓requests可以正常連線?


回答列表

  • 2020/07/28 下午 00:08
    張維元 (WeiYuan)
    贊同數:1
    不贊同數:0
    留言數:1

    嗨,因為比較新的版本網路存取有比較麻煩的限制,會要求有 SSL 的驗證,可以透過以下的方法試試看:


    1. 把 SSL 驗證關掉:


    ```

    requests.get(url='...', verify=False)

    ```


    2. 設定預設的 SSL 憑證:


    ```

    $ pip uninstall -y certifi 

    $ pip install certifi==2015.04.28

    ```


    3. 在程式中指定 SSL 憑證:


    ```

    import ssl

    ssl._create_default_https_context = ssl._create_unverified_context

    # Reference: https://stackoverflow.com/questions/43204012/how-to-disable-ssl-verification-for-urlretrieve

    ```


    如果這個回答對你有幫助請主動點選「有幫助」或「最佳解答」的按鈕,也可以追蹤我的GITHUB 帳號。若還有問題的話,也歡迎再開一個新的問題繼續發問(留言在原本的討論底下我可能不會回覆)或者把你理解的部分整理上來,我都會提供你 Review 和 Feedback 😃😃😃