Python 抓取:SSL:http://en.wikipedia.org 的 CERTIFICATE_VERIFY_FAILED 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50236117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scraping: SSL: CERTIFICATE_VERIFY_FAILED error for http://en.wikipedia.org
提问by Catherine4j
I'm practicing the code from 'Web Scraping with Python', and I keep having this certificate problem:
我正在练习 '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("")
The error is:
错误是:
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)>
Btw,I was also practicing scrapy, but kept getting the problem: command not found: scrapy (I tried all sorts of solutions online but none works... really frustrating)
顺便说一句,我也在练习scrapy,但一直遇到问题:找不到命令:scrapy(我在网上尝试了各种解决方案,但都没有奏效……真令人沮丧)
回答by Jey Miranda
Once upon a time I stumbled with this issue. If you're using macOS go to Macintosh HD > Applications > Python3.6 folder (or whatever version of python you're using) > double click on "Install Certificates.command" file. :D
曾几何时,我偶然发现了这个问题。如果您使用的是 macOS,请转至 Macintosh HD > Applications > Python3.6 文件夹(或您使用的任何版本的 Python)> 双击“Install Certificates.command”文件。:D
回答by Azim
To solve this:
要解决这个问题:
All you need to do is to install Python certificates! A common issue on macOS.
您需要做的就是安装 Python 证书!macOS 上的常见问题。
Open these files:
打开这些文件:
Install Certificates.command
Update Shell Profile.command
Simply Run these two scripts and you wont have this issue any more.
只需运行这两个脚本,您就不会再遇到此问题。
Hope this helps!
希望这可以帮助!
回答by Hemant
For novice users, you can go in the Applications folder and expand the Python 3.7 folder. Now first run (or double click) the Install Certificates.command and then Update Shell Profile.command
对于新手用户,您可以进入 Applications 文件夹并展开 Python 3.7 文件夹。现在首先运行(或双击)安装 Certificates.command,然后更新 Shell Profile.command
回答by Hillsie
This terminal command:
此终端命令:
open /Applications/Python\ 3.7/Install\ Certificates.command
open /Applications/Python\ 3.7/Install\ Certificates.command
Found here: https://stackoverflow.com/a/57614113/6207266
在这里找到:https: //stackoverflow.com/a/57614113/6207266
Resolved it for me. With my config
为我解决了。用我的配置
pip install --upgrade certifi
pip install --upgrade certifi
had no impact.
没有影响。
回答by Rambod
to use unverified ssl you can add this to your code:
要使用未经验证的 ssl,您可以将其添加到您的代码中:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
回答by Alexis Berson
Two steps worked for me : - going Macintosh HD > Applications > Python3.7 folder - click on "Install Certificates.command"
两个步骤对我有用: - 转到 Macintosh HD > Applications > Python3.7 文件夹 - 单击“Install Certificates.command”
回答by Patrick Suzuki
Take a look at this post, it seems like for later versions of Python, certificates are not pre installed which seems to cause this error. You should be able to run the following command to install the certifi package: /Applications/Python\ 3.6/Install\ Certificates.command
看看这篇文章,似乎对于更高版本的 Python,没有预先安装证书,这似乎导致了这个错误。您应该能够运行以下命令来安装 certifi 包:/Applications/Python\ 3.6/Install\ Certificates.command
Post 1: urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error
帖子 1:urllib 和“SSL:CERTIFICATE_VERIFY_FAILED”错误
帖子 2:Airbrake 错误:urlopen 错误 [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获得本地颁发者证书
回答by Amy Mou
For anyone who is using anaconda, you would install the certifi
package, see more at:
对于使用 anaconda 的任何人,您将安装该certifi
软件包,请参阅:
https://anaconda.org/anaconda/certifi
https://anaconda.org/anaconda/certifi
To install, type this line in your terminal:
要安装,请在终端中输入以下行:
conda install -c anaconda certifi
回答by Nitin
Use requests library.
Try this solution, or just add https://
before the URL:
使用请求库。试试这个解决方案,或者https://
在 URL 前添加:
import requests
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
global pages
html = requests.get("http://en.wikipedia.org"+pageUrl, verify=False).text
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("")
Check if this works for you
检查这是否适合您
回答by VIC3KING
If you're running on a Mac you could just search for Install Certificates.command
on the spotlight and hit enter.
如果您在 Mac 上运行,则只需Install Certificates.command
在聚光灯下搜索并按 Enter。