python3.7 urlopen請求HTTPS警告'CERTIFICATE_VERIFY_FAILED'解決辦法


  • 環境: Mac 10.13.6 python3.7

  • 代碼

from urllib.request import urlopen
html = urlopen('https://en.wikipedia.org/wiki/Kevin_Bacon',)
  • 報錯如下urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749) , 大概意思是證書(certificate)驗證失敗

  • 解決辦法:

from urllib.request import urlopen
import ssl
# 導入頭文件

# 生成證書上下文(unverified 就是不驗證https證書)
context = ssl._create_unverified_context()
# 改為如下即可
html = urlopen('https://en.wikipedia.org/wiki/Kevin_Bacon', context=context)
  • 另外一種解決辦法是重寫https默認的驗證方式:
from urllib.request import urlopen
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
html = urlopen('https://en.wikipedia.org/wiki/Kevin_Bacon',)

以上兩種方式選其一即可

這里是requests請求https證書報錯解決辦法: https://www.cnblogs.com/adampei-bobo/p/9414586.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM