Cat*_*e4j 37 python beautifulsoup ssl-certificate scrapy web-scraping
我正在练习'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("")
Run Code Online (Sandbox Code Playgroud)
错误是:
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)>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我也在练习scrapy,但一直都在解决问题:找不到命令:scrapy(我在网上尝试过各种解决方案,但都没有用......真的很令人沮丧)
小智 120
曾几何时我偶然发现了这个问题.如果您正在使用macOS,请转到Macintosh HD>应用程序> Python3.6文件夹(或您正在使用的任何版本的python)>双击"Install Certificates.command"文件.:d
小智 84
要使用未经验证的 ssl,您可以将其添加到您的代码中:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Run Code Online (Sandbox Code Playgroud)
Hem*_*ant 21
对于新手用户,您可以进入 Applications 文件夹并展开 Python 3.7 文件夹。现在首先运行(或双击)安装 Certificates.command,然后更新 Shell Profile.command
小智 15
要解决这个问题:
您需要做的就是安装Python证书!macOS上的常见问题。
打开这些文件:
Install Certificates.command
Update Shell Profile.command
Run Code Online (Sandbox Code Playgroud)
只需运行这两个脚本,您将不再有此问题。
希望这可以帮助!
Ton*_*ony 15
这是在 Windows 电脑上对我有用的唯一解决方案
pip install pip_system_certs
pip install python-certifi-win32
Run Code Online (Sandbox Code Playgroud)
小智 13
open /Applications/Python\ 3.7/Install\ Certificates.command
Run Code Online (Sandbox Code Playgroud)
在终端中尝试这个命令
小智 7
对于使用 anaconda 的任何人,您将安装该certifi
软件包,请参阅:
https://anaconda.org/anaconda/certifi
要安装,请在终端中输入以下行:
conda install -c anaconda certifi
Run Code Online (Sandbox Code Playgroud)
小智 6
看看这篇文章,似乎对于更高版本的Python,没有预先安装证书,这似乎导致了这个错误。您应该能够运行以下命令来安装 certifi 包:/Applications/Python\ 3.6/Install\ Certificates.command
帖子 1:urllib 和“SSL: CERTIFICATE_VERIFY_FAILED”错误
帖子 2:Airbrake 错误:urlopen 错误 [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书
我遇到了同样的错误,并通过运行以下程序代码解决了问题:
# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module. Uses the certificates provided by the certifi package:
# https://pypi.python.org/pypi/certifi
import os
import os.path
import ssl
import stat
import subprocess
import sys
STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
| stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
| stat.S_IROTH | stat.S_IXOTH )
def main():
openssl_dir, openssl_cafile = os.path.split(
ssl.get_default_verify_paths().openssl_cafile)
print(" -- pip install --upgrade certifi")
subprocess.check_call([sys.executable,
"-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])
import certifi
# change working directory to the default SSL directory
os.chdir(openssl_dir)
relpath_to_certifi_cafile = os.path.relpath(certifi.where())
print(" -- removing any existing file or link")
try:
os.remove(openssl_cafile)
except FileNotFoundError:
pass
print(" -- creating symlink to certifi certificate bundle")
os.symlink(relpath_to_certifi_cafile, openssl_cafile)
print(" -- setting permissions")
os.chmod(openssl_cafile, STAT_0o775)
print(" -- update complete")
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
该终端命令:
open /Applications/Python\ 3.7/Install\ Certificates.command
在这里找到:https : //stackoverflow.com/a/57614113/6207266
为我解决了。用我的配置
pip install --upgrade certifi
没有影响。
我可以找到这个解决方案并且工作正常:
cd /Applications/Python\ 3.7/
./Install\ Certificates.command
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42415 次 |
最近记录: |