回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我想将marketstack api的“日内”输出从JSON转换为pd数据帧,但出现以下错误:</p>
<pre><code>data = r.json()['intraday']
KeyError: 'intraday'
</code></pre>
<p>我使用的Python代码:</p>
<pre><code>r = requests.get('http://api.marketstack.com/v1/tickers/AAPL/intraday?access_key=KEY')
x = r.json()
data = r.json()['intraday']
df = pd.DataFrame.from_dict(data)
print(df)
</code></pre>
<p>API(r)的输出:</p>
<pre><code>{'pagination': {'limit': 100, 'offset': 0, 'count': 100, 'total': 4279}, 'data': {'name': 'APPLE INC', 'symbol': 'AAPL', 'has_intraday': True, 'has_eod': False, 'country': None, 'stock_exchange': {'name': 'Investors Exchange', 'acronym': 'IEX', 'mic': 'IEXG', 'country': 'USA', 'country_code': 'US', 'city': 'New York', 'website': 'www.iextrading.com'}, 'intraday': [{'open': 113.73, 'high': 116.55, 'low': 112.88, 'last': 113.84, 'close': 115.04, 'volume': 1293135.0, 'date': '2020-10-26T18:00:00+0000', 'symbol': 'AAPL', 'exchange': 'IEXG'}, {'open': 113.73, 'high': 116.55, 'low': 113.59, 'last': 113.9, 'close': 115.04, 'volume': 1027116.0, 'date': '2020-10-26T17:00:00+0000', 'symbol': 'AAPL', 'exchange': 'IEXG'}, {'open': 113.73, 'high': 116.55, 'low': 113.59, 'last': 114.885, 'close': 115.04, 'volume': 506229.0, 'date': '2020-10-26T15:00:00+0000', 'symbol': 'AAPL', 'exchange': 'IEXG'}, {'open': 113.73, 'high': 115.855, 'low': 113.59, 'last': 115.69, 'close': 115.04, 'volume': 223879.0, 'date': '2020-10-26T14:00:00+0000', 'symbol': 'AAPL', 'exchange': 'IEXG'}, {'open': 113.73, 'high': 113.83, 'low': 113.59, 'last': 113.65, 'close': 115.04, 'volume': 930.0, 'date': '2020-10-26T13:00:00+0000', 'symbol': 'AAPL', 'exchange': 'IEXG'}
</code></pre>
<p>如果有人有解决方案,那就太好了,提前谢谢</p>