【问题标题】:Trying to save API values into variables gives errors尝试将 API 值保存到变量中会出错
【发布时间】:2020-05-29 10:52:31
【问题描述】:

所以我试图从 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: list indices must be integers or slices, not str em>。然后我尝试在两者之间添加 [0],但这给了我错误 IndexError: list index out of range

我尝试使用数组来攻击它,并附加如下值:

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 很有趣,因为它是最新更新的)。

但在显示“金额”、“价格单位”和“订单”时,我遇到了同样的问题。尝试执行 [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

【问题讨论】:

  • 问题是有些商品在["sell_summary" ]中有一个空列表
  • 是的,我知道 API 中有一个项目没有信息,“ENCHANTED_CARROT_ON_A_STICK”,即使他们有更新的项目,他们也没有删除该项目。 “ENCHANTED_CARROT_STICK”。可能是问题所在,我该如何解决?
  • 为什么不使用 ["quick_status"] 端点?
  • “quick_status”显示不正确的买入和卖出价格,其中一些是正确的,但“SUPER_COMPACTOR_3000”等项目的卖出价格(以 priceperunit 为单位)为“497131.9”,并且在 quick_status 中有售价为“488648.9”,差价约10k。以前也有过,后来发现不对! sell_summary 和 buy_summary 有正确的价格@BendikKnapstad
  • 我有一种方法可以在该网站的另一个页面上获取值,但我似乎无法找到将这些值放入变量中的方法,我在另一个页面上使用它:@ 987654323@@BendikKnapstad

标签: python json api flask


【解决方案1】:

试试这个:

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]

【讨论】:

  • 谢谢!我可能已经解决了这个问题,但这可能是一个更好的方法。售价是否存储在“res”中?
  • 是的,它将结果存储在res
  • 我遇到了这个代码的问题:UnboundLocalError: local variable 'sell' referenced before assignment,这是我的代码:pastebin.com/SGZPhesU
  • 你得到的是因为你的代码而不是因为我的代码。无论如何,问题出在您的代码中,否则没有sell变量
  • 我把“res”改成了“sell”?真的不知道这会如何改变输出。
【解决方案2】:

好的,我为你准备了这个:

你要先检查sell_status是否为空:

for item in products:
    if products[item]["sell_summary"]:
        print(f'{item} sells for: {products[item]["sell_summary"][-1]["pricePerUnit"]}')

【讨论】:

  • 我会试一试的!我试图在没有任何信息的情况下删除该产品,它似乎可以工作,如果我遇到问题,我会试试这个!
  • 顺便说一句,我不知道您是否想要 sell_summary 列表中的最后一个或第一个条目,如果您想要第一个条目,请将 [-1] 更改为 [0]
  • 是的,我让我的工作在这里,唯一的问题是我制作的“脚本”,用于计算用户将为每个产品获得的利润似乎不起作用。我必须尝试更多的东西,如果我无法弄清楚,我想我必须对此进行讨论。感谢您告诉我那个没有信息的项目,这就是问题所在!
猜你喜欢
  • 2019-05-08
  • 2013-07-08
  • 2011-03-20
  • 2011-11-10
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多