首页 > 编程问答 >SSL 证书验证失败 - 雅虎财经 API - Python

SSL 证书验证失败 - 雅虎财经 API - Python

时间:2024-07-28 14:31:35浏览次数:8  
标签:python ssl yahoo-finance certifi

我正在尝试从雅虎财经获取数据,但收到 SSL 错误。代码如下:

import requests
response = requests.get("https://query1.finance.yahoo.com/v8/finance/chart/META", verify=True)
print(response.status_code)

出现以下错误:

urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/urllib3/connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/urllib3/util/retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='query1.finance.yahoo.com', port=443): Max retries exceeded with url: /v8/finance/chart/META (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/yudhveersingh/Desktop/Algo Trade/test", line 3, in <module>
    response = requests.get("https://query1.finance.yahoo.com/v8/finance/chart/META", verify=True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/adapters.py", line 517, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='query1.finance.yahoo.com', port=443): Max retries exceeded with url: /v8/finance/chart/META (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)')))

我尝试绕过验证过程:

import requests
response = requests.get("https://query1.finance.yahoo.com/v8/finance/chart/META", verify=False)
print(response.status_code)

但是我收到了 429 错误代码:

/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/urllib3/connectionpool.py:1103: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
  warnings.warn(
429

遇到的错误是由于 Yahoo Finance API 的变化以及 Python 处理 SSL 证书的方式造成的。

错误分析:

  1. SSLError: CERTIFICATE_VERIFY_FAILED : 这个错误表明的 Python 环境无法验证 Yahoo Finance 服务器提供的 SSL 证书。这可能是因为的 Python 版本过旧,或者的系统缺少最新的根证书。

  2. InsecureRequestWarning 429 错误 : 当设置 verify=False 时,绕过了 SSL 验证,这会产生安全风险。Yahoo Finance 服务器检测到未经验证的请求并返回 429 错误 (Too Many Requests),这可能是因为他们限制未经验证的请求以防止滥用。

解决方案:

  1. 更新 Python 和证书 :

  2. 确保使用的是最新版本的 Python。

  3. 使用 pip 更新 certifi urllib3 库,这些库管理 SSL 证书:

    bash pip install --upgrade certifi urllib3 * 如果问题仍然存在,可能需要手动更新的系统根证书。请查阅的操作系统文档以获取有关如何执行此操作的说明。

  4. 使用 Yahoo Finance API 的官方库 :

  5. Yahoo Finance 提供了官方的 Python 库,例如 yfinance ,它可以处理 SSL 证书验证和其他 API 细节:

    ```python import yfinance as yf

    data = yf.download("META", period="1d") print(data) ```

    使用官方库通常是获取 Yahoo Finance 数据的最可靠和最简单的方法。

重要提示:

  • 避免在生产环境中禁用 SSL 验证 ( verify=False ),因为它会带来安全风险。
  • 使用官方库或确保的 Python 环境和证书是最新的,以便安全可靠地访问 Yahoo Finance API。

标签:python,ssl,yahoo-finance,certifi
From: 78166871

相关文章

  • 【学习笔记】Matlab和python双语言的学习(熵权法)
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、熵权法的基本概念二、熵权法的基本步骤1、构建决策矩阵2、数据标准化3、计算指标的比重4、计算信息熵5、计算权重6、计算综合得分三、代码实现----Matlab四、代码实现----python总结......
  • 【python】网络通信编程例子
    以下是一个简单的Python示例,展示了如何在Linux下使用套接字进行基本的网络通信,包括创建服务器和客户端。服务器端代码importsocket#创建一个IPv4TCP套接字server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#绑定服务器地址和端口server_addr......
  • 如何将Python版本从3.9降级到3.7?
    我正在开发RaspberryPi。这些是我的操作系统信息:pi@raspberrypi:~$uname-marmv7lpi@raspberrypi:~$cat/etc/os-releasePRETTY_NAME="RaspbianGNU/Linux11(bullseye)"NAME="RaspbianGNU/Linux"VERSION_ID="11"VERSION="11(bullseye)......
  • Python终端输出彩色字符方法
    colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,完全兼容linux和windows各个版本。 1.Python3.x中安装colorama模块: pipinstallcolorama'''可用格式常数:【颜色RED,GREEN都需要大写】Fore:BLACK,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHI......
  • 我无法安装 pygame 模块,所以我尝试观看视频,它告诉我这样做。在那个视频中他得到了 pyt
    c:\User\admin>piplistSyntaxError:unexpectedcharacterafterlinecontinuationcharacter我试图获取python模块列表,但出现语法错误出现SyntaxError:unexpectedcharacterafterlinecontinuationcharacter错误是因为你的用户名中包含一个特殊字符......
  • python第五节--conda命令
    这里写自定义目录标题基本命令环境管理包管理环境文件环境变量Conda配置高级操作常见问题基本命令检查Conda版本:conda--version更新Conda:condaupdateconda环境管理创建新环境:condacreate--namemyenv创建包含特定Python版本的新环境:conda......
  • 如何使用python向另一台计算机发送请求
    基本上我有一个聊天室,我将把它变成一个网络(我知道这听起来没有多大意义),但基本上我想知道是否可以让python脚本捕获计算机上的所有传出请求并将其发送到另一台计算机(c2)。然后我希望c2自己发出请求。这是对我正在做的事情的淡化解释,但任何帮助都会很棒!当然可以!虽然从头......
  • AttributeError:'int'对象没有属性'index'(python)
    我正在Python上进行“猜单词”,但我无法弄清楚这个错误。AttributeError:'int'objecthasnoattribute'index'(python)它在线上给了我一个错误letterIndex=word.index(guess)defcheckLetter(word):blanks='_'*len(str(word))print('W......
  • 尝试在Python中使用for循环来输出大于或等于序列中的数字
    这是我的Python代码:largest_so_far=-1print('before',largest_so_far)forthe_numin[9,41,12,3,74,15]:ifthe_num>largest_so_far:largest_so_far=the_numprint(largest_so_far,'isbiggerthan',the_num)......
  • 如何在 wxPython 的 for 循环中添加文本输入框?
    我是wxPython的新手,正在开发一个带有GUI的基本程序,让用户标记图像。现在,当用户单击“导入”按钮时,他们可以选择一个目录。然后,代码使用matplotlib在for循环中显示该目录中的每个图像。但是,我不知道如何在for循环中访问用户输入。这就是该函数现在的样子:importmatplo......