简体   繁体   中英

How can I consume real-time data from alphavantage API in python's dictionary?

I try to use alphavantage API in my project. At the moment I am going to parse the JSON data in this way:

from alpha_vantage.timeseries import TimeSeries

def AlphaVantage(symbol):
    ts = TimeSeries(key='mykey')
    data = ts.get_intraday(symbol, interval='1min')

    print(str(data))


AlphaVantage('MSFT')

I would like to get only the most current data.

If the API does not provide it, you can just grab the first one as the data is obviously sorted by date:

print(str(data['Time Series (1min)'][0]))

Hope it helps!

The data returned is a tuple. There are two dictionaries inside the tuple. The first one contains the time and high/low values and the second contains the metadata. So, you can access the time:high/low values by data[0]. As you know that the keys in a dictionary are unsorted, so to only get the most recent data, you can use max of keys of this dictionary. So, the final code would be like this: print(str(data[0][max(data[0].keys())]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM