尝试将API值保存到变量中会出现错误

问题描述 投票:0回答:2

因此,我正在尝试从API中的项目中保存Sellprice和Buyprice。该API包含190多种产品,并且有一个“ sell_summary”和“ buy_summary”,在每个“ sell_summary”和“ buy_summary”中都有一个“ pricePerUnit”,这就是我要访问的内容,保存为一个变量。值是浮点的,我可以用它来访问它们并将它们保存在变量中(productNames是具有所有产品名称的数组):

for x in productNames:
        sell_Price = f["products"][x]["sell_summary"]

这不起作用,因为它给了我:

{'amount': 62, 'pricePerUnit': 1280.2, 'orders': 1}

而且我只对“ pricePerUnit”感兴趣,我也试图在“ sell_summary”之后有一个[“ pricePerUnit],但这给了我错误TypeError:列表索引必须是整数或切片,而不是str。然后尝试在两者之间添加[0],但这给了我错误IndexError:列表索引超出范围

我尝试使用数组来代替它,并像这样附加值:

sell_Price = []
    for x in productNames:
        sell_Price.append(f["products"][x]["sell_summary"][:1])

((obs:我必须做[:1],因为API可以包含多个带有pricePerUnit的“部分”,可以在此pastebin中看到:https://pastebin.com/XwW4aQVR |仅顶部的“ pricePerUnit很有趣,因为这是最新更新的)” 。

但是它显示了“金额”,“ pricePerUnit”和“订单”,这给了我同样的问题。尝试执行[0] [“ pricePerUnit],但出现了与以前相同的错误!

[0]["pricePerUnit"] = IndexError: list index out of range
[sell_summary][pricePerUnit] = TypeError: list indices must be integers or slices, not str

我需要保存这些“ pricePerUnit”值以通过我拥有的脚本运行它,该脚本应该计算(取决于用户输入)他们可以购买每种产品的多少,以及通过购买然后出售可以赚多少钱。它!

API链接:https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2

python json api flask
2个回答
0
投票

尝试一下:

import requests

data = requests.get('https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2').json()
res = [product['sell_summary'][0]['pricePerUnit'] for product in data['products'].values() if product['sell_summary']]
print(res)

输出

[4.6, 13.3, 3.1, 66.7, 5.2, 652.6, 1039.6, 2638.9, 105560.6, 9641.0, 1130.5, 2428.0, 345.3, 838.4, 4.3, 476128.5, 4000.6, 3.8, 2005.7, 3.4, 1579.8, 8530.1, 3.9, 2510.4, 932.3, 1903.2, 15.0, 16.6, 23000.3, 7.3, 4422.7, 4.9, 26.7, 10.6, 2356.7, 3.5, 3550.5, 927.4, 22144.9, 11.0, 10.3, 1.7, 3.8, 29.4, 3.0, 595.5, 3.9, 84493.3, 19.1, 1.9, 1266.9, 62904.3, 2226.3, 111750.6, 1753.7, 881.9, 15275.3, 5.9, 667.2, 1.4, 1242.4, 8.7, 8.2, 511.2, 99.3, 785.8, 0.5, 8822.6, 7003.0, 38.2, 13.6, 225.7, 190.7, 1086.1, 1479.1, 236269.6, 1914.4, 63817.0, 207238.8, 1067.1, 421.3, 79499.2, 1.9, 2.8, 81384.8, 360458.7, 13.1, 386.3, 176250.8, 47973.2, 9.1, 17.6, 8.6, 21.1, 1339.3, 98003.3, 2.1, 0.7, 195.8, 22.7, 669.0, 14586.9, 3165.9, 1805.2, 640.1, 47433.5, 5.4, 25891.6, 6.0, 5.4, 591.9, 19435.8, 213667.3, 141335.7, 252100.5, 49.0, 5.0, 20.5, 6776.4, 194.2, 9103.6, 127661.0, 189429.9, 1380.1, 2651.8, 5.7, 152892.4, 473.1, 12.6, 10.4, 35312.0, 95945.5, 1873.9, 497131.4, 232073.2, 1106.2, 6700.7, 5960.9, 369.5, 1689.1, 205200.0, 236353.8, 5.7, 3580.4, 102838.4, 491.9, 4.9, 2639.7, 5108.3, 23.3, 275.1, 2.9, 19293.3, 3704.1, 1100.0, 9.7, 1571.7, 7.8, 175352.9, 391.6, 292445.9, 12.8, 4521.1, 18.6, 24818.1, 6.9, 4498.7, 5.7, 7.4, 37421.6, 642.8, 171720.1, 471.9, 1014.4, 13.9, 3070.3, 14.0, 16.7, 9.8, 785.3, 633.6, 8.1, 718454.0, 22.0, 1270.5, 164062.4, 16.8, 232260.1, 550.2, 57.8, 1750.4, 2800.7, 541161.8, 9.6, 100743.6, 6004.7, 1253.1]

0
投票

好,所以我为您准备了这个:

您必须首先检查sell_status是否为空:

for item in products:
    if products[item]["sell_summary"]:
        print(f'{item} sells for: {products[item]["sell_summary"][-1]["pricePerUnit"]}')
© www.soinside.com 2019 - 2024. All rights reserved.