Как извлечь данные из Riot Games API? - PullRequest
0 голосов
/ 07 мая 2018

Я все еще новичок и только начал работать с Python. Я пытаюсь получить уровень и рейтинг игрока с помощью API Riot Games (только EUW) через JSON, но получаю исключение:

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

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

Я не знаю, что мне нужно изменить, может быть, кто-то может мне помочь :) Код:

import requests

def requestSummonerData(summonerName, APIKey):

    URL = "https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + summonerName + "?api_key=" + APIKey
    print (URL)
    response = requests.get(URL)
    return response.json()

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


def main():

    summonerName = (str)(input('Type your Summoner Name here: '))
    APIKey = (str)(input('Copy and paste your API Key here: '))
    responseJSON  = requestSummonerData(summonerName, APIKey)

    print(responseJSON)
    ID = responseJSON ['id']
    ID = str(ID)
    print (ID)
    responseJSON2 = requestRankedData(ID, APIKey)
    print (responseJSON2[ID][0]['tier'])
    print (responseJSON2[ID][0]['entries'][0]['rank'])
    print (responseJSON2[ID][0]['entries'][0]['leaguePoints'])


if __name__ == "__main__":
    main()

1 Ответ

0 голосов
/ 07 мая 2018

responseJSON2 является list. Список имеет индексы (0, 1, 2, ...).

Вам нужно использовать int для вашего списка:

ID = str(ID)

не так, вам нужно иметь int там!

попробуйте с

ID = int(ID)

И вы можете преобразовать обратно в строку:

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()

Вам нужно найти индекс, соответствующий вашему идентификатору, в вашем ответе:

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'])
...