[英]Trying to access items from an API gives errors
所以我试图从 API 返回一个浮点值,它位于: products => product_name => sell/buy_summary => pricePerUnit
并且可以在这个 API 中找到: https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2
当我将它返回到我的 HTML 时,我收到错误“TypeError:列表索引必须是整数或切片,而不是 str”,我在 Google 上搜索了一下,还看到了 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)
该脚本适用于 arrays,所以我需要从 API 中获取所有“pricePerUnit”,每个产品(190+)并将其存储在数组中,这就是我希望能够存储“ pricePerUnit”首先在变量中(sellPrice 和 buyPrice),然后是 append 到两个不同的 arrays,这样我就可以通过它运行我的脚本了!
OBS:我将需要每个产品的“sell_summary”和“buy_summary”的“pricePerUnit”!
谢谢
由于buySummary
和sellSummary
是 arrays,所以需要使用数组索引从第一个元素获取价格。
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"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.