python3.7 urlopen请求HTTPS警告'CERTIFICATE_VERIFY_FAILED'解决办法

  • 环境: Mac 10.13.6 python3.7html

  • 代码python

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)验证失败url

  • 解决办法:code

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证书报错解决办法: http://www.javashuo.com/article/p-fimuzvmy-ec.htmlhtm

相关文章
相关标签/搜索