我正在练习“使用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("")

错误是:

  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(我尝试了各种在线解决方案,但没有一个工作…真的令人沮丧)


当前回答

遗憾的是,我没有解决这个问题。 但设法使代码工作(顺便说一句,几乎所有我的代码都有这个问题) 本地颁发者证书问题发生在python3.7下 所以我改回python2.7 QAQ 所有这些都需要改变,包括“from urllib2 import urlopen”而不是“from urllib”。请求导入urlopen" 如此悲伤…

其他回答

我正在使用Debian 10 buster,并尝试用youtube-dl下载一个文件,并得到这个错误: sudo youtube-dl -k https://youtu.be/uscis0CnDjk

[youtube] uscis0CnDjk:下载网页 <urlopen ERROR [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Unable to get local issuer certificate (_ssl.c:1056)>(由URLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Unable to get local issuer certificate (_ssl.c:1056)')引起))

正确安装了python2和python3.8的证书,但我仍然收到相同的错误。 最后(这不是最好的解决方案,但对我来说是消除证书检查,因为它是youtube-dl中的一个选项)使用这个命令 sudo youtube-dl -k——no-check-certificate https://youtu.be/uscis0CnDjk

要使用未经验证的SSL,可以在代码中添加:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

我在窗户上使用anaconda。得到相同的错误,直到我尝试以下;

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

这是我从stackoverflow线程中使用urlopen得到的:

Python urllib urlopen不工作

有两步对我很有效: 导入Macintosh HD > Applications > Python3.7文件夹 -点击“Install Certificates.command”

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

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

一旦我将REQUESTS_CA_BUNDLE设置为空白(即从.bash_profile中删除),请求就会再次工作。

export REQUESTS_CA_BUNDLE=""

该问题仅在通过CLI(命令行接口)执行python请求时出现。如果我运行请求。get(URL, CERT)它解决得很好。

Mac OS Catalina(10.15.6)。 3.6.11的Pyenv。 我正在获得的错误消息:[SSL: CERTIFICATE_VERIFY_FAILED]证书验证失败:无法获得本地颁发者证书(_ssl.c:1056)

我的答案是:https://stackoverflow.com/a/64151964/4420657