【问题标题】:Trying to access items from an API gives errors尝试从 API 访问项目会出错
【发布时间】:2020-05-28 15:41:20
【问题描述】:

所以我试图从 API 返回一个浮点值,它位于: products => product_name => sell/buy_summary => pricePerUnit

并且可以在这个 API 中找到: https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2

当我将它返回到我的 HTML 时,我收到错误“TypeError:列表索引必须是整数或切片,而不是 str”,我搜索了一下,也看到了 Stackoverflow 上的另一个线程,但没有让它工作。我有另一种方法来获取完全正常的值,但问题是我需要将这些值保存到变量中,通过它运行脚本来计算利润/利润。

这是我当前的代码:

@app.route('/bflipper', methods=['POST', 'GET'])
def bFlipper():
    f = requests.get(
        'https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2').json()
    product_name = []
    product_sellPrice = []
    product_buyPrice = []
    for x in productNames:
        product_name.append(f["products"][x]["product_id"])
        product_buyPrice.append(
            f["products"][x]["buy_summary"]["pricePerUnit"])
        product_sellPrice.append(
            f["products"][x]["sell_summary"]["pricePerUnit"])
    if request.method == 'POST':
        userInput = request.form['coins']
        return render_template("flipper.html", userInput=userInput, product_name=product_name, product_buyPrice=product_buyPrice, product_sellPrice=product_sellPrice)
    else:
        return render_template("flipper.html", product_name=product_name, product_buyPrice=product_buyPrice, product_sellPrice=product_sellPrice)

这个脚本虽然适用于数组,所以我需要从 API 中获取所有“pricePerUnit”,每个产品 (190+) 并将其存储在数组中,这就是我希望能够首先将“pricePerUnit”存储在变量中(sellPrice 和 buyPrice),然后将其附加到两个不同的数组中,这样我就可以通过它运行我的脚本了!

OBS:我将需要每个产品的“sell_summary”和“buy_summary”的“pricePerUnit”!

谢谢

【问题讨论】:

  • sell_summarybuy_summary 是数组。所以有多个价格,你想用它们做什么?
  • 您想将哪个pricePerUnit 附加到product_sellPriceproduct_buyPrice
  • @Barmar "sell_summary" 和 "buy_summary"(在 API 内部)由一个 "pricePerUnit" 组成,它是销售和购买产品的价格。我需要将“sell_summary”和“buy_summary”的价格保存在一个变量中,稍后我可以将其用于脚本。
  • 你看过 JSON 了吗?它由一个数组组成,如[{"amount":20181,"pricePerUnit":4.1,"orders":1},{"amount":28630,"pricePerUnit":4.0,"orders":1},{"amount":35037,"pricePerUnit":3.9,"orders":1},{"amount":140326,"pricePerUnit":3.8,"orders":2}]
  • 有 4 个不同的pricePerUnit 用于不同的金额。您希望变量中的哪一个?

标签: python json flask


【解决方案1】:

由于buySummarysellSummary是数组,所以需要使用数组索引从第一个元素获取价格。

for x in productNames:
    if x in f["products"]:
        product_name.append(f["products"][x]["product_id"])
        if len(f["products"][x]["buy_summary"]) > 0:
            product_buyPrice.append(
                f["products"][x]["buy_summary"][0]["pricePerUnit"])
        if len(f["products"][x]["sell_summary"]) > 0:
            product_sellPrice.append(
                f["products"][x]["sell_summary"][0]["pricePerUnit"])

【讨论】:

  • 我试过这样做:pastebin.com/d722cTqs |但我收到错误:IndexError: list index out of range on "f["products"][x]["buy_summary"][0]["pricePerUnit"])",我之前也遇到过这个问题,当我尝试在 buy/sell_summary 和 "pricePerUnit" 之间使用 "[0]"
  • 可能其中一些有空数组,你需要检查一下。
  • 我添加了检查产品名称是否在列表中,并且至少有一个价格。
  • 我让它工作了!忘了“检查答案”,我的错!不过谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
相关资源
最近更新 更多