我试图使用python从web获取数据。我导入了urllib。请求包,但在执行时,我得到错误:

certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)

我在Mac OS High Sierra上使用Python 3.7。 我试图从CSV文件: https://s3.amazonaws.com/assets.datacamp.com/production/course_1606/datasets/winequality-red.csv

当我将URL更改为“http”时-我能够获得数据。但是,我认为这避免了检查SSL证书。

所以我在网上找到了一个解决方案: 运行/Applications/Python\ 3.7/Install\ certificates .命令

这解决了我的问题。但是我没有SSL之类的知识。你能帮我理解一下它到底是怎么解决我的问题的吗?

如果可能的话,请给我推荐一些好的资源来了解安全与证书。我是新手。

谢谢!

注意:我确实通过链接openssl, python请求错误:“证书验证失败”

我的问题与链接中的问题不同,因为我想知道当我安装certifi包或运行install \ Certificates.command来修复错误时实际发生了什么。我对证券的理解很差。


当前回答

创建一个从操作系统证书到Python的符号链接对我来说很有效:

ln -s /etc/ssl/* /Library/Frameworks/Python.framework/Versions/3.9/etc/openssl

(我在macOS上,使用pyenv)

其他回答

我愿意提供一份参考资料。我使用cmd +空格,然后键入Install Certificates.command,然后按Enter。过了一会儿,弹出命令行界面开始安装。

 -- removing any existing file or link
 -- creating symlink to certifi certificate bundle
 -- setting permissions
 -- update complete

最后,它修复了错误。

对于那些仍然想知道如何解决这个问题的人,我通过安装“Install Certificates.command”得到了我的答案。

我是这样做的,

只需双击该文件,等待它安装,在我的情况下,你就准备好了

在开始时粘贴以下代码:

# paste this at the start of code
import ssl 

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

简单地运行:

PIP install——trustedhost =pypi.org——trustedhost =files.pythonhosted.org——user PIP -system-certs'

由于这个问题没有[macos]标签,我在ubuntu下发布了一个同样问题的解决方案:

sudo apt install ca-certificates
sudo update-ca-certificates --fresh
export SSL_CERT_DIR=/etc/ssl/certs

解决方案来自Github上的Zomatree。