我正在尝试从雅虎财经获取数据,但收到 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 证书的方式造成的。
错误分析:
-
SSLError: CERTIFICATE_VERIFY_FAILED
: 这个错误表明的 Python 环境无法验证 Yahoo Finance 服务器提供的 SSL 证书。这可能是因为的 Python 版本过旧,或者的系统缺少最新的根证书。 -
InsecureRequestWarning
和429
错误 : 当设置verify=False
时,绕过了 SSL 验证,这会产生安全风险。Yahoo Finance 服务器检测到未经验证的请求并返回429
错误 (Too Many Requests),这可能是因为他们限制未经验证的请求以防止滥用。
解决方案:
-
更新 Python 和证书 :
-
确保使用的是最新版本的 Python。
-
使用
pip
更新certifi
和urllib3
库,这些库管理 SSL 证书:bash pip install --upgrade certifi urllib3
* 如果问题仍然存在,可能需要手动更新的系统根证书。请查阅的操作系统文档以获取有关如何执行此操作的说明。 -
使用 Yahoo Finance API 的官方库 :
-
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。