从Python中的JSON API提取财务数据

2024-09-30 20:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,我正在尝试从API中提取特定数据

例如,我想提取3y_夏普比。我试过:

import requests
import json

VTI = requests.get("https://eodhistoricaldata.com/api/fundamentals/VTI.US?api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX").json()

print(VTI["3y_SharpRatio"])

但收到以下错误:

KeyError                                  
Traceback (most recent call last) <ipython-input-7-4dc0323003b9> in <module> 
----> 1 print(VTI["3y_SharpRatio"]) 
KeyError: '3y_SharpRatio' 

欢迎任何帮助,如果这是一个非常基本的问题,请道歉


Tags: 数据httpsimportcomapijsongetrequests
2条回答

您在文章中包含了您的API密钥,从安全角度来看这是不好的,但确实允许我看到3y_SharpRatio在这里:

ETF_Data    
    Performance 
        3y_Volatility   "12.37"
        3y_ExpReturn    "0.00"
        3y_SharpRatio   "1.03"
        Returns_3Y  "14.14"

所以你需要

print(VTI['ETF_Data']['Performance']['3y_SharpRatio']
import requests
import json

VTI = requests.get("https://eodhistoricaldata.com/api/fundamentals/VTI.US?api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX").json()
# print (json.dumps(VTI, indent = 20))
# print (json.dumps(VTI["ETF_Data"], indent = 20))
# print (json.dumps(VTI["ETF_Data"]['Performance'], indent = 20))
print (VTI["ETF_Data"]["Performance"]['3y_SharpRatio'])
'1.03'

我已经注释掉了几行,你可以用它们一步一步地挖下去找到你的钥匙

相关问题 更多 >