SSL: CERTIFICATE_VERIFY_FAILED

错误描述

出错的代码

from urllib import request
headers = { 'User-Agent': 'Mozilla/5.0 Chrome/64.0.3282.186 Safari/537.36', }
url = 'https://xxxxxxxxx'
req = request.Request(url, headers=headers)
response = request.urlopen(req)
data = response.read().decode('UTF-8')
print(data)

解决方法

from urllib import request
import ssl                     # 新增
headers = { 'User-Agent': 'Mozilla/5.0 Chrome/64.0.3282.186 Safari/537.36', }
url = 'https://xxxxxxxxx'
context = ssl._create_unverified_context()       # 新增
req = request.Request(url, headers=headers)
response = request.urlopen(req, context=context)        # 修改
data = response.read().decode('UTF-8')
print(data)

参考

https://blog.csdn.net/babybabyup/article/details/81044277