我正在练习“使用Python进行Web抓取”中的代码,我一直遇到这个证书问题:
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(我在网上尝试了各种解决方案,但都没有成功。。。真令人沮丧)
此终端命令:
open /Applications/Python\ 3.7/Install\ Certificates.command
在此处找到: https://stackoverflow.com/a/57614113/6207266
帮我解决了。 使用我的配置
pip install --upgrade certifi
没有影响。
有一次,我偶然发现了这个问题。如果您正在使用macOS,请转到Macintosh HD>;应用程序>;Python3.6文件夹(或您正在使用的任何版本的python)>;双击“Install Certificates.command”文件。:天
要解决这个问题:
您只需要安装Python证书!macOS上的常见问题。
打开这些文件:
只要运行这两个脚本,就不会再有这个问题了。
希望这有帮助!
相关问题 更多 >
编程相关推荐