所以我试图从API中返回一个浮动值,它位于:products => product_name => sellbuy_summary => pricePerUnit。
并可以在这个API中找到。https:/api.hypixel.netskyblockbazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2。
当我把它返回到我的HTML中时,我得到了 "TypeError: list indices must be integers or slices, not 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"。
谢谢你
自 buySummary
和 sellSummary
是数组,你需要使用数组索引从第一个元素获取价格。
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"])