如何组织来自 API 源的数据从 Python 输出到 Excel?

How to organize data from API source to output from Python into Excel?

我正在使用 requests(见下文)调用 API 源并获取一些数据(见下文)。我不知道如何处理它。

它看起来像 JSON and/or 字典,但我不知道如何在导出到 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()

您现在有一个名为 obj 的 python 对象,它是嵌套字典的复杂结构。这在 python 中很容易操作,但它看起来肯定不像 表格 ,因此尝试将其导出到 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')

注意 json.dump 调用中的 obj['averages']。此外,我使用 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