如何从Riot Games API中提取数据?

2024-05-15 02:02:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我还是一个初学者,刚刚开始使用Python。 我试图通过JSON获取Riot Games(只有EUW)API的玩家的等级和等级,但我得到了一个例外:

print (responseJSON2[ID][0]['tier'])

TypeError: list indices must be integers or slices, not str

我不知道我要改变什么,也许有人可以帮我:) 代码:

^{pr2}$

Tags: apiidjson玩家gameslistriottier
1条回答
网友
1楼 · 发布于 2024-05-15 02:02:08

responseJSON2list。列表有索引(0,1,2,…)。在

列表需要使用int:

ID = str(ID)

是错的,你需要有个智力测验!在

试试看

^{pr2}$

你可以转换回字符串:

def requestRankedData(ID, APIKey):
    URL= "https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/"+str(ID)+"?api_key="+APIKey
    print (URL)
    response = requests.get(URL)
    return response.json()

您需要在响应中找到与您的ID匹配的索引:

responseJSON2 = requestRankedData(ID, APIKey)
ID_idx = responseJSON2.index(str(ID))
print (responseJSON2[ID_idx][0]['tier'])
print (responseJSON2[ID_idx][0]['entries'][0]['rank'])
print (responseJSON2[ID_idx][0]['entries'][0]['leaguePoints'])

相关问题 更多 >

    热门问题