I am creating a tracking program and apparently the code works fine but the problem is that it depends on the name it changes and gives such an error
This is my code:
# 1. Descargar HTML y buscar accountID
import requests
import re
#Agrego la participación del usuario
name = input("Ingresa tu ID -> ")
part1 = "https://fortnitetracker.com/profile/all/"
part = "/competitive"
link = part1 + name + part
r = requests.get(link)
uid = re.search(r'"accountId": "([0-9a-f-]+)"', r.text).group(1)
# 2. Acceder a la API y obtener JSON con estadísticas
r = requests.get(f"https://fortnitetracker.com/api/v0/profile/{uid}/stats?season=16&isCompetitive=true")
data = r.json()
stats = data[2]["stats"]["all"]
for e in stats:
print(f"{e['metadata']['name']:>16s}: {e['displayValue']}")
This is the error :
IndexError: list index out of range
Why don't I search in google or youtube if it is a common topic? That is why I have searched for two means and the videos that I have found have been of no help since they are different cases.
I hope your help, thank you very much
Why are you looking
2
for the answer elementdata
? The IndexError tells you just that, the index2
does not exist. The response must be a json (not a list), and its elements can be accessed by name.If you put a
print(data)
just before declaringstats
you should be able to see the json, and you will surely see that there is no element with the key2
.The solution would be:
change the line of
By:
Thank you very much to those who spent time with my problem