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

新手上路,请多包涵

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

错误是:

   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(我在网上尝试了各种解决方案,但都没有奏效……真的很沮丧)

原文由 Catherine4j 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 825
2 个回答

曾几何时,我偶然发现了这个问题。如果您使用的是 macOS,请转到 Macintosh HD > Applications > Python3.6 文件夹(或您使用的任何版本的 Python)> 双击“Install Certificates.command”文件。 :D

原文由 Jey Miranda 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

 import ssl
ssl._create_default_https_context = ssl._create_unverified_context

原文由 Rambod 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题