You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手调用Riot Games(EUW) API遇索引错误,求解决方法

解决Riot API调用中的TypeError问题

嘿,我来帮你搞定这个报错!首先咱们先拆解一下你遇到的错误:TypeError: list indices must be integers or slices, not str,这个错误的核心是你把列表当成字典来用了,用字符串(也就是你的ID变量)去索引列表,这肯定不行,列表只能用整数或者切片来定位元素。

问题根源:Riot API的返回结构

你调用的两个API返回数据的结构是不一样的:

  • 召唤师信息API(/lol/summoner/v3/summoners/by-name/)返回的是单个字典对象,里面包含召唤师的id、name等信息;
  • 段位信息API(/lol/league/v3/positions/by-summoner/{summonerId})返回的是列表,因为一个玩家可能在多个队列(比如单双排、灵活组排)有段位,列表里的每个元素就是对应一个队列的段位字典。

你错误地写了responseJSON2[ID][0]['tier'],这里的responseJSON2本身就是列表,不能用ID(召唤师ID)去索引它,这完全搞反了结构!

修正后的代码示例

我把你的逻辑补全并修正,你可以参考:

import requests

def requestSummonerData(summonerName, APIKey):
    # 1. 获取召唤师基本信息
    summoner_url = f"https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/{summonerName}?api_key={APIKey}"
    summoner_response = requests.get(summoner_url)
    summoner_data = summoner_response.json()
    
    # 提取召唤师ID
    summoner_id = summoner_data['id']
    
    # 2. 获取该召唤师的段位信息
    league_url = f"https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/{summoner_id}?api_key={APIKey}"
    league_response = requests.get(league_url)
    league_data = league_response.json()  # 这里返回的是列表
    
    # 处理段位数据:遍历所有队列的段位,或者取第一个(比如单双排)
    if league_data:
        # 示例:打印第一个队列的段位和排名
        first_queue_tier = league_data[0]['tier']
        first_queue_rank = league_data[0]['rank']
        print(f"段位:{first_queue_tier},排名:{first_queue_rank}")
        
        # 如果想筛选特定队列(比如单双排),可以这样:
        for queue_data in league_data:
            if queue_data['queueType'] == 'RANKED_SOLO_5x5':
                print(f"单双排段位:{queue_data['tier']},排名:{queue_data['rank']}")
    else:
        print("该玩家暂无段位信息")

# 调用函数,替换成你的召唤师名称和API密钥
requestSummonerData("你的召唤师名称", "你的Riot API密钥")

关键修正点

  • 去掉了错误的responseJSON2[ID][0]写法,直接用league_data[0]来访问列表里的第一个段位对象;
  • 增加了对返回数据是否为空的判断(避免玩家没打排位时出错);
  • 补充了筛选特定队列段位的逻辑,更实用。

内容的提问来源于stack exchange,提问作者dolu28

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:51:59