爬虫:针对http://en.wikipedia.org的SSL证书验证失败错误

284
我正在练习《Python网络数据采集》中的代码,但我一直遇到这个证书问题:

I'm practicing the code from 'Web Scraping with Python', and I keep having this certificate problem:

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("")

错误信息为:

  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)>

顺便说一下,我也在练习Scrapy,但总是遇到这个问题:找不到命令:scrapy(我尝试了各种在线解决方案,但都没有用……真的很沮丧)


1
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1049)> - Catherine4j
2
还有...请告诉我这个错误的原因,真的很想知道~~谢谢!! - Catherine4j
3
529个现有的SSL: CERTIFICATE_VERIFY_FAILED问题,请找出你的解决方案,然后将其关闭为重复。 - smci
我正要评论显而易见的事情:你是用https而不是http访问它了吗? - smci
1
在我的Mac OS Big Sur上,export SSL_CERT_DIR=/etc/ssl/certs 对我起了作用。 - TrigonaMinima
显示剩余2条评论
27个回答

0

对我来说,问题在于我在我的.bash_profile中设置了REQUESTS_CA_BUNDLE

/Users/westonagreene/.bash_profile:
...
export REQUESTS_CA_BUNDLE=/usr/local/etc/openssl/cert.pem
...

一旦我将REQUESTS_CA_BUNDLE设置为空白(即从.bash_profile中删除),requests就可以正常工作了。

export REQUESTS_CA_BUNDLE=""

这个问题只在通过命令行界面(CLI)执行Python的requests时出现。如果我运行requests.get(URL, CERT),它就能正常解决。

Mac OS Catalina(10.15.6)。 Pyenv版本为3.6.11。 我收到的错误信息是:[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

我的回答在其他地方:https://dev59.com/MVQJ5IYBdhLWcg3w66fM#64151964


0

我遇到了一个问题,Python 似乎在尝试使用一个不存在的 cert.pem 文件。可以通过运行以下命令来查看:

import ssl
paths = ssl.get_default_verify_paths()

openssl_cafile 指向的路径 /etc/ssl/cert.pem 在该路径下不存在。

SSL_CERT_FILE 设置为存在的路径即可解决问题:

export SSL_CERT_FILE=/etc/pki/tls/cert.pem

0
你可以尝试这个:
import ssl 

ssl._create_default_https_context = ssl._create_unverified_context  

先检查重复项 -> https://dev59.com/M1UL5IYBdhLWcg3whIS-#60671292 谢谢 - HedgeHog

-1

顺便说一下,如果你使用 aiohttp 时遇到相同的错误,只需将 verify_ssl=False 参数放入你的 TCPConnector 中:

import aiohttp
...

async with aiohttp.ClientSession(
    connector=aiohttp.TCPConnector(verify_ssl=False)
) as session:
    async with session.get(url) as response:
        body = await response.text()

-1
我在Ubuntu 18.04上的Python 3.7环境中遇到了"_ssl.c:1091"的错误。没有提到的解决方法对我起作用。最后,通过试错的方式,当我创建了一个新的环境,使用了3.11.5版本的Python以及相关的库,还有conda 11.7,问题得到了解决。

你的回答可以通过提供更多的支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好的回答的更多信息。 - undefined

-1

这个方法可行。将环境变量PYTHONHTTPSVERIFY设置为0。

  • 通过输入Linux命令:
export PYTHONHTTPSVERIFY = 0

OR

  • 在Python代码中使用:
import os
os.environ["PYTHONHTTPSVERIFY"] = "0"

这完全没有帮助! - 钟智强

-2

我在Windows上使用Anaconda。在尝试以下方法之前,一直遇到相同的错误:

import urllib.request
link = 'http://docs.python.org'
with urllib.request.urlopen(link) as response:
    htmlSource = response.read()

我从stackoverflow的帖子中获取了以下内容,关于使用urlopen:

Python urllib urlopen not working


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接