我正在尝试使用 python 从 iex api 下载数据,目前我已经到了获取数据的地步,但现在我想对其进行格式化。基本上我得到了很多我不关心的数据,我只想拥有“浮动”部分。数据应如下所示:股票代码,浮动,AAPL,4700000000,(类似的)我正在使用的代码:import requests url = "https://api.iextrading.com/1.0/stock/aapl/stats" response = requests.get(url).json()print (response)如果有人能解释我如何做到这一点,我会非常高兴。亲切的问候现在我有代码:import requests url = "https://api.iextrading.com/1.0/stock/aapl/stats" response = requests.get(url).json()data = (response['symbol'], response['float'])import json filename='resp.json'with open(filename, 'a+') as outfile: json.dump(data, outfile, indent=4)import requests url = "https://api.iextrading.com/1.0/stock/tsla/stats" response = requests.get(url).json()data = (response['symbol'], response['float'])import json filename='resp.json'with open(filename, 'a+') as outfile: json.dump(data, outfile, indent=4)我希望数据显示为:股票代码,浮动,苹果, 4700000000,TSLA, 1700000000,(不必将 Ticker 和 float 放在上面,反正我可以在 excel power query 中自己做)。
2 回答
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
您的代码正在做它应该做的事情,如果您想要 json 的某个部分,只需访问它。
import requests
url = "https://api.iextrading.com/1.0/stock/aapl/stats"
response = requests.get(url).json()
print(response['float'])
>4705473314
print(response['symbol'])
>'AAPL'
print(response['symbol'], response['float'])
要存储response在 json 文件中,我们可以执行以下操作
import json
filename='resp.json'
with open(filename, 'w') as outfile:
json.dump(response, outfile, indent=4)
森栏
TA贡献1810条经验 获得超5个赞
你可以把它当作一本字典。response['float']会给你浮动。对于任何键也是如此。
import requests
url = "https://api.iextrading.com/1.0/stock/aapl/stats"
response = requests.get(url).json()
print (response['float'])
print(response['symbol'])
输出
4705473314
AAPL
添加回答
举报
0/150
提交
取消