我正在练习“使用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(我尝试了各种在线解决方案,但没有一个工作…真的令人沮丧)
我正在使用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
我也有同样的错误,并通过运行下面的程序代码解决了这个问题:
# 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()