gpt4 book ai didi

python - 抓取 https ://www. thenewboston.com/时出现 "SSL: certificate_verify_failed"错误

转载 作者:IT老高 更新时间:2023-10-28 20:51:28 26 4
gpt4 key购买 nike

所以我最近开始使用 youtube 上的“The New Boston's”视频学习 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] 证书验证失败 (_ssl.c:645)

如果这是一个愚蠢的问题,我深表歉意,我还是编程新手,但我真的无法弄清楚这一点,我正考虑跳过本教程,但无法解决这个问题让我很困扰,谢谢!

最佳答案

问题不在于您的代码,而在于您尝试访问的网站。在查看 analysis by SSLLabs 时你会注意到:

This server's certificate chain is incomplete. Grade capped to B.

这意味着服务器配置错误,不仅python,还有其他几个在这个站点上都会有问题。一些桌面浏览器通过尝试从 Internet 加载丢失的证书或填充缓存的证书来解决此配置问题。但其他浏览器或应用程序也会失败,类似于 python。

要解决损坏的服务器配置,您可以显式提取丢失的证书并将它们添加到您的信任库中。或者您可以在 verify 参数中将证书作为信任提供。来自 the documentation :

You can pass verify the path to a CA_BUNDLE file or directory withcertificates of trusted CAs:

>>> requests.get('https://github.com', verify='/path/to/certfile') 

This list of trusted CAs can also be specified through theREQUESTS_CA_BUNDLE environment variable.

关于python - 抓取 https ://www. thenewboston.com/时出现 "SSL: certificate_verify_failed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34503206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com