python - 如何将 API 源中的数据组织到 Python 输出到 Excel 中?
问题描述
我正在使用requests
(见下文)调用 API 源并获取一些数据(见下文)。我不确定如何处理它。
它看起来像 JSON 和/或字典,但我不知道在导出到 Excel 之前如何在 Python 中处理它。我明白,我在这个阶段还没有导入 JSON,但我可以。
我的代码:
url = 'https://quoteapi.com/api/v5/symbols/hvn.asx?appID=af5f4d73c1a54a33&averages=1&fundamentals=1&liveness=delayed'
result = requests.get(url, headers=headers)
返回的数据:
{
"symbol": "hvn.asx",
"realSymbol": "hvn.asx",
"units": "price",
"currency": "AUD",
"tz": "AEST",
"tickTable": "0.0005<0.1,0.0025<2,0.005",
"quote": {
"pctChange": 0.556,
"quoteBases": {
},
"low": 5.39,
"open": 5.4,
"vwap": 5.430336243,
"value": 16256099.035,
"time": "2021-07-07 16:49:15",
"change": 0.03,
"price": 5.43,
"status": "ADJUST_ON",
"close": 5.43,
"bid": 5.43,
"prevClose": 5.4,
"lastTradeTime": "2021-07-07 16:49:15",
"ask": 5.44,
"volume": 2993572,
"high": 5.48,
"tradeCount": 5260
},
"sequence": 6502147,
"desc": {
"firstActive": "1990-08-06",
"underlyingIssuerSymbol": "hvn.asx",
"issuerName": "Harvey Norman Holdings Ltd",
"isin": "AU000000HVN7",
"securityType": "01",
"shortDesc": "FPO",
"lastActive": "2021-07-07",
"abbrevDesc": "ORDINARY",
"longDesc": "ORDINARY FULLY PAID",
"shortName": "HARVEY",
"underlyingIssuerNdividendPerShare": 0.38,
"earningsPerShare": 0.5857
},
"averages": {
"ytd": {
"vwap": 5.395,
"tradeCount": 4418,
"high": 6.09,
"value": 18583818.22,
"volume": 3431631,
"low": 4.7,
"open": 4.69
},
"year": {
"vwap": 4.884,
"tradeCount": 4240,
"high": 6.09,
"value": 18055781.32,
"volume": 3734356,
"low": 3.49,
"open": 3.59
},
"month": {
"vwap": 5.293,
"tradeCount": 4322,
"high": 5.69,
"value": 20414631.17,
"volume": 3865196,
"low": 5.07,
"open": 5.36
},
"week": {
"vwap": 5.508,
"tradeCount": 3979,
"high": 5.69,
"value": 19188403.29,
"volume": 3486067,
"low": 5.37,
"open": 5.4
}
}
}
解决方案
继续您的代码,您可以使用以下requests
方法直接解析 json 字符串:
obj = result.json()
您现在有一个名为 的python 对象obj
,它是嵌套字典的复杂结构。这在 python 中很容易操作,但它看起来肯定不像tabular,因此尝试将其导出到 Excel 没有多大意义。此外,正如评论中所述,您没有指出结果应该是什么样子。
但是,averages
数据中的键似乎是表格的,以下是如何从中创建数据框并将其导出到 excel:
import json
with open("foobar.json", "r") as f:
obj = json.load(f)
with open("averages.json", "w") as f:
json.dump(obj['averages'], f)
df = pd.read_json('averages.json').T
df.to_excel('averages.xlsx')
注意通话obj['averages']
中的。json.dump
另外,我使用该T
函数来转置数据框,它看起来更自然(但这是可选的。这是数据框的样子:
In [5]: df
Out[5]:
vwap tradeCount high value volume low open
ytd 5.395 4418.0 6.09 18583818.22 3431631.0 4.70 4.69
year 4.884 4240.0 6.09 18055781.32 3734356.0 3.49 3.59
month 5.293 4322.0 5.69 20414631.17 3865196.0 5.07 5.36
week 5.508 3979.0 5.69 19188403.29 3486067.0 5.37 5.40
推荐阅读
- mysql - 正确配置 MyBatis OUT 参数到存储过程调用
- c# - 有没有办法使用字符串在循环中声明新变量?
- struct - Option 类型是否小于包装类型加上布尔值?
- r - 删除具有负值的行时出现奇怪的结果
- puppeteer - 如何创建与 puppeteer 页面完全一样的 PDF?
- solr - Solr 中的 Edismax 查询对不同的字段使用不同的字符串
- typescript - 摆脱Vscode的cmd点击内联窗口,直接导航到代码
- flutter - PopMenu dropDown onChanged 不改变视图上的当前值
- android - Oncomplete 任务在 getResult() 之后无法访问方法 getDownloadUrl()
- multiprocessing - astropy 多处理访问 IERS 数据