Bil*_*ins 19 python ssl ssl-certificate web-scraping
所以我最近在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)
Run Code Online (Sandbox Code Playgroud)
完整的错误是: ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)
如果这是一个愚蠢的问题我很抱歉,我仍然是编程的新手,但我真的无法弄清楚这一点,我正在考虑跳过这个教程,但是我无法解决这个问题,谢谢!
mat*_*exx 17
您可以告诉请求不要验证SSL证书:
>>> url = "https://www.thenewboston.com/forum/category.php?id=15&orderby=recent&page=1"
>>> response = requests.get(url, verify=False)
>>> response.status_code
200
Run Code Online (Sandbox Code Playgroud)
Ste*_*ich 16
问题不在您的代码中,而是在您尝试访问的网站中.在查看SSLLabs的分析时,您会注意到:
此服务器的证书链不完整.等级上限为B.
这意味着服务器配置是错误的,不仅python而且其他几个都会遇到此站点的问题.某些桌面浏览器通过尝试从Internet加载缺少的证书或使用缓存的证书填写来解决此配置问题.但是其他浏览器或应用程序也会失败,类似于python.
要解决损坏的服务器配置,您可以显式提取缺少的证书并将其添加到信任存储区.或者您可以在verify参数中将证书作为trust.从文档:
您可以使用受信任CA的证书验证路径到CA_BUNDLE文件或目录:
Run Code Online (Sandbox Code Playgroud)>>> requests.get('https://github.com', verify='/path/to/certfile')此可信CA的列表也可以通过REQUESTS_CA_BUNDLE环境变量指定.
如果要使用 Python dmg 安装程序,还必须阅读 Python 3 的自述文件并运行 bash 命令以获取新证书。
尝试跑步
/Applications/Python\ 3.6/Install\ Certificates.command
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79800 次 |
| 最近记录: |