You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Yahoo Finance API遇SSL证书验证失败及429错误求助

解决Yahoo Finance API调用的SSL及429错误问题

1. 处理SSL证书验证失败(自签名证书)问题

不要直接用verify=False绕过验证,这会带来安全风险,优先用以下两种合规方法:

  • 指定本地可信证书
    将你的自签名证书保存为.pem格式文件,在请求时指定证书路径:
import requests

# 替换为实际的Yahoo Finance API接口
api_url = "https://query1.finance.yahoo.com/v7/finance/quote?symbols=META"
# 替换为你的自签名证书本地路径
cert_path = "/path/to/your/self-signed-cert.pem"

response = requests.get(api_url, verify=cert_path)
  • 将证书添加到系统可信存储
    • Windows:打开「证书管理器」,把证书导入到「受信任的根证书颁发机构」
    • macOS:打开「钥匙串访问」,将证书拖入「系统」钥匙串,右键设置「始终信任」
    • Linux:复制证书到/usr/local/share/ca-certificates/,执行sudo update-ca-certificates更新系统证书库

2. 解决429请求超限及InsecureRequestWarning警告

429状态码处理

429是Yahoo的请求频率限制,解决方法:

  • 添加浏览器请求头
    模拟真实浏览器的请求标识,避免被识别为爬虫:
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
}
response = requests.get(api_url, headers=headers, verify=cert_path)
  • 控制请求频率
    添加请求间隔,避免短时间内多次调用:
import time

# 每次请求后延迟1秒
time.sleep(1)
response = requests.get(api_url, headers=headers, verify=cert_path)

消除InsecureRequestWarning(仅在必须禁用验证时使用)

如果暂时无法配置证书,必须用verify=False,可以关闭警告:

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

# 禁用不安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

response = requests.get(api_url, verify=False, headers=headers)

⚠️ 注意:禁用证书验证会暴露请求被篡改的风险,仅作为临时方案。

内容的提问来源于stack exchange,提问作者yudhveer singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 19:32:43