gpt4 book ai didi

python - 无法获取json中存储的某些项目的对应值

转载 作者:行者123 更新时间:2023-12-01 02:08:32 24 4
gpt4 key购买 nike

我用 python 编写了一个脚本,用于从网页的 json 响应中获取不同的项目。我通过收集不同项目的名称取得了部分成功。但是,我希望获得不同的价格。当谈到解析每个项目的相应值时,我陷入了困境。对此的任何帮助将不胜感激。

站点地址:web_link

我尝试过的脚本:

import requests

res = requests.get("replace_with_above_url")
for items in res.json():
for name in items:
print(name)

结构如下:

[{"BTC":{"sellPrice":711500,"buyPrice":711150,"lastTradePrice":711150}},{"XRP":{"sellPrice":76.7,"buyPrice":76.6,"lastTradePrice":76.6}},{"NEO":{"sellPrice":8769,"buyPrice":8651,"lastTradePrice":8769}},{"GAS":{"sellPrice":3140,"buyPrice":3105,"lastTradePrice":3105}},{"ETH":{"sellPrice":63500,"buyPrice":62450.01,"lastTradePrice":63500}},{"XLM":{"sellPrice":30.78,"buyPrice":30.61,"lastTradePrice":30.78}}]

我的输出(仅):

BTC
XRP
NEO

我想要得到的输出:

BTC 711500 711150 711150
XRP 76.7 76.6 76.6
so on ---

最佳答案

使用这个:

r = requests.get('https://bitbns.com/order/getTickerAll')
for item in r.json():
for key, value in item.items():
print(key, value['sellPrice'], value['buyPrice'], value['lastTradePrice'])

输出:

BTC 705000 704000 704000
XRP 72.3 72.29 72.29
NEO 8452 8450 8452
GAS 3060 3024 3024
ETH 61000 60700 60700
XLM 29.8 29.78 29.78

来自documentation :

When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.

关于python - 无法获取json中存储的某些项目的对应值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48850130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com