抓取https://www.thenewboston.com/时出现“ SSL:certificate_verify_failed”错误
问题内容:
因此,我最近开始使用youtube上的“新波士顿”视频来学习Python,直到我进入他的制作简单网络抓取工具的教程之前,一切都很好。尽管我完全理解它,但是在运行代码时,所有错误似乎都基于“
SSL:CERTIFICATE_VERIFY_FAILED”。自昨晚以来,我一直在寻找答案,试图找出解决方法,似乎视频或他网站上的评论中没有其他人和我有同样的问题,甚至使用他的其他人的代码网站我得到相同的结果。我将从我从网站上获得的代码中发布代码,因为它给了我同样的错误,而我编写的代码现在很乱。
import requests
from bs4 import BeautifulSoup
def trade_spider(max_pages):
page = 1
while page <= max_pages:
url = "https://www.thenewboston.com/forum/category.php?id=15&orderby=recent&page=" + str(page) #this is page of popular posts
source_code = requests.get(url)
# just get the code, no headers or anything
plain_text = source_code.text
# BeautifulSoup objects can be sorted through easy
for link in soup.findAll('a', {'class': 'index_singleListingTitles'}): #all links, which contains "" class='index_singleListingTitles' "" in it.
href = "https://www.thenewboston.com/" + link.get('href')
title = link.string # just the text, not the HTML
print(href)
print(title)
# get_single_item_data(href)
page += 1
trade_spider(1)
完整的错误是: ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)
如果这是一个愚蠢的问题,我深表歉意,我仍然是编程的新手,但我真的无法弄清楚,我只是想跳过本教程,但是这困扰着我无法解决此问题,谢谢!
问题答案:
问题不在您的代码中,而在您尝试访问的网站中。查看SSLLabs进行的分析时,您会注意到:
该服务器的证书链不完整。等级上限为B。
这意味着服务器配置错误,不仅python,还有其他几个站点也有问题。一些台式机浏览器通过尝试从Internet加载丢失的证书或填充缓存的证书来解决此配置问题。但是其他浏览器或应用程序也会失败,类似于python。
要解决损坏的服务器配置,您可以显式提取丢失的证书并将其添加到信任库中。或者,您也可以在verify参数中将证书授予信任。从文档中:
您可以使用受信任的CA证书来验证CA_BUNDLE文件或目录的路径:
>>> requests.get('https://github.com', verify='/path/to/certfile')
也可以通过REQUESTS_CA_BUNDLE环境变量指定此受信任CA的列表。