Python打開https鏈接報(bào)錯(cuò):unable to get local issuer certificate 或安裝Python3.10以上版本,需要openssl 1.1.1以上版本,會導(dǎo)致openssl 下面缺少證書,記錄下安裝方法
問題描述:
當(dāng)使用urllib.urlopen打開一個(gè) https 鏈接時(shí)拋出如下異常:
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)>
原因:
本地證書不存在。
解決方法:
1、查看默認(rèn)證書位置
import ssl
print(ssl.get_default_verify_paths())
執(zhí)行結(jié)果:
DefaultVerifyPaths(cafile=None, capath=None, openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/Library/Frameworks/Python.framework/Versions/3.7/etc/openssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/Library/Frameworks/Python.framework/Versions/3.7/etc/openssl/certs')
由結(jié)果可見,ca文件不存在。
2、下載ca文件
http://curl.haxx.se/ca/cacert.pem
3、將 cacert.pem 改成 cert.pem 放在證書指定目錄:/Library/Frameworks/Python.framework/Versions/3.7/etc/openssl/cert.pem