刮刮:SSL:http://en.wikipedia.org的CERTIFICATE_VERIFY_FAILED错误

Cat*_*e4j 37 python beautifulsoup ssl-certificate scrapy web-scraping

我正在练习'Web Scraping with Python'的代码,我一直有这个证书问题:

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
import re

pages = set()
def getLinks(pageUrl):
    global pages
    html = urlopen("http://en.wikipedia.org"+pageUrl)
    bsObj = BeautifulSoup(html)
    for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):
        if 'href' in link.attrs:
            if link.attrs['href'] not in pages:
                #We have encountered a new page
                newPage = link.attrs['href'] 
                print(newPage) 
                pages.add(newPage) 
                getLinks(newPage)
getLinks("")
Run Code Online (Sandbox Code Playgroud)

错误是:

  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1049)>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我也在练习scrapy,但一直都在解决问题:找不到命令:scrapy(我在网上尝试过各种解决方案,但都没有用......真的很令人沮丧)

小智 120

曾几何时我偶然发现了这个问题.如果您正在使用macOS,请转到Macintosh HD>应用程序> Python3.6文件夹(或您正在使用的任何版本的python)>双击"Install Certificates.command"文件.:d

  • 传奇答案 (15认同)
  • 我该如何在 Windows 10 上执行此操作?不存在这样的文件。 (12认同)
  • 请参阅@Rambod 的回答,它很快就为我解决了。`import ssl ssl._create_default_https_context = ssl._create_unverified_context` 就是这样,它的工作就像一个魅力。是的,可能存在安全问题,但对于一次性脚本和已知来源......谁给出了 * (10认同)
  • 有人请将此标记为解决它的答案。当我陷入困境时,您为我节省了很多时间和精力!! (6认同)
  • 如果使用Homebrew安装Python,则该文件不存在。解决方案在这里:/sf/answers/3125461531/ (4认同)
  • Windows 10 解决方案是什么? (3认同)
  • 哦,我的Mac上没有这个Install Certificates.command文件...不知道为什么使用TAT (2认同)

小智 84

要使用未经验证的 ssl,您可以将其添加到您的代码中:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Run Code Online (Sandbox Code Playgroud)

  • 这个答案可能应该提到,这段代码可能会带来巨大的安全问题,具体取决于用户在抓取数据后对数据所做的操作。 (5认同)
  • 这很好,因为如果我需要添加的只是两行代码,为什么我必须进行系统更改。 (4认同)
  • 1 行:`导入 ssl;ssl._create_default_https_context = ssl._create_unverified_context`谢谢@Rambod (3认同)
  • 这对我来说是一种临时措施 (2认同)

Hem*_*ant 21

对于新手用户,您可以进入 Applications 文件夹并展开 Python 3.7 文件夹。现在首先运行(或双击)安装 Certificates.command,然后更新 Shell Profile.command

在此处输入图片说明


小智 15

要解决这个问题:

您需要做的就是安装Python证书!macOS上的常见问题。

打开这些文件:

Install Certificates.command
Update Shell Profile.command
Run Code Online (Sandbox Code Playgroud)

只需运行这两个脚本,您将不再有此问题。

希望这可以帮助!

  • 我在我的应用程序目录中没有看到 Python 目录。我正在使用 Anaconda 并为 python 3.6 创建了一个新的环境,因为我之前安装了 python 2.7(这显然也没有在我的应用程序目录中创建一个文件夹!)。 .. 在我的 Mac 上找不到 Certificates.command (10认同)
  • 为什么这个答案甚至得到了赞成?它没有指定这些命令应该在哪里运行,并且缺少有关这“两个脚本”的信息 (5认同)
  • 这些应该从哪里运行? (4认同)
  • @Azim:在 Ubuntu 中怎么样? (2认同)

Ton*_*ony 15

这是在 Windows 电脑上对我有用的唯一解决方案

pip install pip_system_certs
pip install python-certifi-win32
Run Code Online (Sandbox Code Playgroud)


小智 13

open /Applications/Python\ 3.7/Install\ Certificates.command
Run Code Online (Sandbox Code Playgroud)

在终端中尝试这个命令


VIC*_*ING 12

Install Certificates.command如果您在 Mac 上运行,则只需在聚光灯下搜索并按 Enter 键即可。


小智 7

对于使用 anaconda 的任何人,您将安装该certifi软件包,请参阅:

https://anaconda.org/anaconda/certifi

要安装,请在终端中输入以下行:

conda install -c anaconda certifi
Run Code Online (Sandbox Code Playgroud)


小智 6

看看这篇文章,似乎对于更高版本的Python,没有预先安装证书,这似乎导致了这个错误。您应该能够运行以下命令来安装 certifi 包:/Applications/Python\ 3.6/Install\ Certificates.command

帖子 1:urllib 和“SSL: CERTIFICATE_VERIFY_FAILED”错误

帖子 2:Airbrake 错误:urlopen 错误 [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书


Mil*_*vić 6

我遇到了同样的错误,并通过运行以下程序代码解决了问题:

# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module.  Uses the certificates provided by the certifi package:
#       https://pypi.python.org/pypi/certifi

import os
import os.path
import ssl
import stat
import subprocess
import sys

STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
             | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
             | stat.S_IROTH |                stat.S_IXOTH )


def main():
    openssl_dir, openssl_cafile = os.path.split(
        ssl.get_default_verify_paths().openssl_cafile)

    print(" -- pip install --upgrade certifi")
    subprocess.check_call([sys.executable,
        "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])

    import certifi

    # change working directory to the default SSL directory
    os.chdir(openssl_dir)
    relpath_to_certifi_cafile = os.path.relpath(certifi.where())
    print(" -- removing any existing file or link")
    try:
        os.remove(openssl_cafile)
    except FileNotFoundError:
        pass
    print(" -- creating symlink to certifi certificate bundle")
    os.symlink(relpath_to_certifi_cafile, openssl_cafile)
    print(" -- setting permissions")
    os.chmod(openssl_cafile, STAT_0o775)
    print(" -- update complete")

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)


小智 5

两个步骤对我有用: - 转到 Macintosh HD > Applications > Python3.7 文件夹 - 单击“Install Certificates.command”


Hil*_*sie 5

该终端命令:

open /Applications/Python\ 3.7/Install\ Certificates.command

在这里找到:https : //stackoverflow.com/a/57614113/6207266

为我解决了。用我的配置

pip install --upgrade certifi

没有影响。


Ale*_*aro 5

我可以找到这个解决方案并且工作正常:

cd /Applications/Python\ 3.7/
./Install\ Certificates.command
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

42415 次

最近记录:

5 年,9 月 前