我正在使用django框架构建某种股票市场Web应用程序。我从Alpha Vantage API获取数据,在解析所需数据时遇到了问题
1-我可以成功调用API,但在尝试获取所需数据时总是会出错。请参阅我在views.py
上使用的代码:
def home(request):
import requests
import json
import pandas as pd
from alpha_vantage.timeseries import TimeSeries
url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=B3SA3.SA&outputsize=compact&apikey=XXX"
api_request = requests.post("GET", url)
try:
api = api_request.content.json()
except Exception as e:
api="Erro, tente novamente"
return render(request,'home.html', {'api': api})
在home.html
上,我正在使用此代码显示信息还是错误:
{% if api %}
{% if api == "Erro, tente novamente."%}
Houve um problema com a busca da ação, tente novamente.
{% else %}
{% for key,value in api.items %}
{{key}}: {{value}}<br/>
{%endfor%}
{% endif %}
{% endif %}
通过这段代码,我得到了以下内容,正如您所看到的,有两个单独的字典元数据和时间序列(每日):
{“元数据”:{1.信息:'Daily Time Series with Splits and Distribution Events','2.符号':'B3SA3.SA','3.上次刷新':'2020-07-10','4.输出大小':'Compact','5.时区':'US/Eastern'},'Time Series(Daily):{'2020-07-10':{'1.开盘':'58.8000','2.高':'59.9800','3.低':'57.6000','4.收盘':'59.9500','5.调整后收盘':'59.9500','6.成交量':'7989500','7.股息金额':'0.0000','8.分割系数':'1.0000','2020-07-09':{'1.开盘':'60.9700','2.高':'60.9700','3.低':'58.4400','4.收盘':'58.8900','5.调整后收盘':'58.8900','6.成交量':'13494000','7.股息金额':'0.0000','8.分割系数':'1.0000','2020-07-08':{'1.开盘':'57.6100','2.高':'60.8900','3.低':'57.2300','4.收盘':'60.6500','5.调整后收盘':'60.6500','6.成交量':'13847100','7.股息金额':'0.0000','8.分割系数':'1.0000','2020-07-07':{'1.开盘':'56.5500','2.高':'57.6000','3.低':'56.2500','4.收盘':'57.1700','5.调整后收盘':'57.1700','6.成交量':'9038800','7.股息金额':'0.0000','8.分割系数':'1.0000'}
我只是尝试获取'Time Series(Daily)并将其解析为数据帧,但在尝试调用'Time Series(Daily)字典时,总是会出错
你们知道我可能做错了什么吗? 提前谢谢各位
由于未访问“Time Series Daily()”键,导致出现错误
现在,如果您想将这些数据解析成熊猫,可以在DataFrame类中使用from_dict方法。请参见下面的示例
编辑 您的问题的解决方案如下:
德扬戈
在home.html中
相关问题 更多 >
编程相关推荐