gpt4 book ai didi

python - 如何从 JSON 打印特定内容

转载 作者:行者123 更新时间:2023-11-28 22:45:14 25 4
gpt4 key购买 nike

这是 Python 2 从 API 请求数据的代码

 results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/ano64pm6?apikey=9ummN7C6KMHu9aErm49ixoy2ZySmaKCm"))
print results

这打印

{u'count': 27,
u'frequency': u'Daily',
u'lastrunstatus': u'success',
u'lastsuccess': u'Fri Feb 27 2015 21:33:02 GMT+0000 (UTC)',
u'name': u'mmoga',
u'newdata': True,
u'nextrun': u'Sat Feb 28 2015 21:33:03 GMT+0000 (UTC)',
u'results': {u'collection1': [{u'price': u'\xa3\xa03.47'},
{u'price': u'\xa3\xa05.20'},
{u'price': u'\xa3\xa06.92'},
{u'price': u'\xa3\xa08.63'},
{u'price': u'\xa3\xa010.34'},
{u'price': u'\xa3\xa012.04'},
{u'price': u'\xa3\xa013.73'},
{u'price': u'\xa3\xa015.42'},
{u'price': u'\xa3\xa017.09'},
{u'price': u'\xa3\xa025.59'},
{u'price': u'\xa3\xa034.05'},
{u'price': u'\xa3\xa042.48'},
{u'price': u'\xa3\xa050.88'},
{u'price': u'\xa3\xa059.23'},
{u'price': u'\xa3\xa067.56'},
{u'price': u'\xa3\xa075.85'},
{u'price': u'\xa3\xa084.11'},
{u'price': u'\xa3\xa0100.73'},
{u'price': u'\xa3\xa0117.27'},
{u'price': u'\xa3\xa0133.75'},
{u'price': u'\xa3\xa0150.17'},
{u'price': u'\xa3\xa0166.51'},
{u'price': u'\xa3\xa0182.79'},
{u'price': u'\xa3\xa0198.99'},
{u'price': u'\xa3\xa0215.13'},
{u'price': u'\xa3\xa0231.20'},
{u'price': u'\xa3\xa0247.28'}]},
u'thisversionrun': u'Fri Feb 27 2015 21:32:48 GMT+0000 (UTC)',
u'thisversionstatus': u'success',
u'version': 3}

我需要调用 price 并获取旁边的 float 。到目前为止,我已经尝试了一些非常简单的事情,例如,

print requests[price]

还有

print requests[results/collection1/price]

我不知道该怎么做,我是编程新手,我找不到任何有用的教程,所以只要指出正确的方向就会有所帮助

最佳答案

import  requests
results = requests.get("https://www.kimonolabs.com/api/ano64pm6?apikey=9ummN7C6KMHu9aErm49ixoy2ZySmaKCm").json()
print([x["price"] for x in results["results"]["collection1"]])
['£\xa03.47', '£\xa05.20', '£\xa06.92', '£\xa08.63', '£\xa010.34', '£\xa012.04', '£\xa013.73', '£\xa015.42', '£\xa017.09', '£\xa025.59', '£\xa034.05', '£\xa042.48', '£\xa050.88', '£\xa059.23', '£\xa067.56', '£\xa075.85', '£\xa084.11', '£\xa0100.73', '£\xa0117.27', '£\xa0133.75', '£\xa0150.17', '£\xa0166.51', '£\xa0182.79', '£\xa0198.99', '£\xa0215.13', '£\xa0231.20', '£\xa0247.28']

您可以直接使用requests 获取json调用 .json()

print(results["results"]["collection1"])

[{'price': '£\xa03.47'}, {'price': '£\xa05.20'}, {'price': '£\xa06.92'}, {'price': '£\xa08.63'}, {'price': '£\xa010.34'}, {'price': '£\xa012.04'}, {'price': '£\xa013.73'}, {'price': '£\xa015.42'}, {'price': '£\xa017.09'}, {'price': '£\xa025.59'}, {'price': '£\xa034.05'}, {'price': '£\xa042.48'}, {'price': '£\xa050.88'}, {'price': '£\xa059.23'}, {'price': '£\xa067.56'}, {'price': '£\xa075.85'}, {'price': '£\xa084.11'}, {'price': '£\xa0100.73'}, {'price': '£\xa0117.27'}, {'price': '£\xa0133.75'}, {'price': '£\xa0150.17'}, {'price': '£\xa0166.51'}, {'price': '£\xa0182.79'}, {'price': '£\xa0198.99'}, {'price': '£\xa0215.13'}, {'price': '£\xa0231.20'}, {'price': '£\xa0247.28'}]

results["results"]["collection1"] 返回字典列表,然后在迭代时使用键 price 从每个字典中获取价格列表。

关于python - 如何从 JSON 打印特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28775481/

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