import requests
import json
url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo"
response = requests.get(url)
data = json.loads(response.text)
import pandas as pd
df = pd.read_json(json.dumps(data['Time Series (Daily)']), orient='index')
df.index.name = 'date'
其中,需要注意的是,Alpha Vantage API返回的JSON数据中时间戳为键名,需要使用orient='index'参数指定JSON的键名作为每行的索引。
最终得到的数据框的前几行如下所示:
1. open 2. high 3. low 4. close 5. adjusted close 6. volume 7. dividend amount 8. split coefficient
date
2021-02-26 235.0000 235.8100 227.3500 231.6000 231.6000 34754505 0.000 1.000
2021-02-25 235.6100 236.3900 226.7300 228.9900 228.9900 39068159 0.000 1.000
2021-02-24 230.3200 235.5500 230.0000 235.1400 235.1400 25494000 0.000 1.000
2021-02-23 223.7600 228.1500 222.4201 226.2300 226.2300 31537199 0.000 1.000
202