Python调用Hypixel SkyBlock Bazaar API报TypeError错误求助
报错排查与解决方案
报错原因
- 问题核心是对API返回的JSON结构理解错误:
dictionary['products']['BROWN_MUSHROOM']['sell_summary']是列表类型,而非字典,你直接用字符串键['sellPrice']访问列表,触发了类型不匹配的报错。 - 可以自行添加一行代码
print(type(dictionary['products']['BROWN_MUSHROOM']['sell_summary']))验证,输出结果为<class 'list'>。 - 该列表存储多组挂单数据,按售价从低到高排序,每一组数据才是包含
sellPrice、amount等字段的字典,列表的第0项对应当前最低卖价。
解决方法
修改打印逻辑,先取列表的指定下标(通常取下标0的最低卖价),再访问sellPrice字段即可,修正后的完整代码如下:
import requests import json # 本次代码未用到BeautifulSoup,可删除该行导入 link = requests.get('https://api.hypixel.net/skyblock/bazaar') data = link.text dictionary = json.loads(data) # 先取sell_summary列表的第0项,再访问对应字段 print(str(dictionary['products']['BROWN_MUSHROOM']['sell_summary'][0]['sellPrice']))
补充说明
如果需要遍历所有卖单的售价,可以用循环遍历sell_summary列表:
for sell_order in dictionary['products']['BROWN_MUSHROOM']['sell_summary']: print(sell_order['sellPrice'])
内容的提问来源于stack exchange,提问作者Magic Tax
相关产品推荐
相关产品推荐

