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


当前回答

有一次,我被这个问题绊倒了。如果你使用macOS,进入Macintosh HD > Applications > Python3.6文件夹(或任何你使用的python版本)>双击“Install Certificates.command”文件。: D

其他回答

要解决这个问题:

你所需要做的就是安装Python证书!macOS的一个常见问题。

打开以下文件:

Install Certificates.command
Update Shell Profile.command

简单地运行这两个脚本,你就不会再有这个问题了。

希望这能有所帮助!

对于新手用户,您可以进入Applications文件夹并展开Python 3.7文件夹。现在首先运行(或双击)安装证书。命令,然后更新Shell配置文件。命令

如果你在Mac上运行,你可以在聚光灯下搜索Install Certificates.command,然后按enter键。

有一次,我被这个问题绊倒了。如果你使用macOS,进入Macintosh HD > Applications > Python3.6文件夹(或任何你使用的python版本)>双击“Install Certificates.command”文件。: D

终端命令:

打开/Applications/Python\ 3.7/Install\ Certificates.command

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

帮我解决了。 用我的配置

PIP安装——升级证书

没有影响。