Skip to content
Advertisement

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

So, I have this piece of code:

grade = session.get('https://api-ege.interneturok.ru/api/v2/journal/pages?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvYXBpLXBzcC5pbnRlcm5ldHVyb2sucnVcL2FwaVwvdjJcL3Rva2VucyIsImlhdCI6MTY2NDk5MzQ0NiwiZXhwIjoxNjY0OTk3MDQ2LCJuYmYiOjE2NjQ5OTM0NDYsImp0aSI6IjE3OUpMYmhIb3ByYnZOdjIiLCJzdWIiOjI5MTY0NDIsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.RfPkDzTOKawwAQTp_4v6XU-hQgOazmFb7rkv80naT1w')

It gives me this response:

'{"pages":[{"id":7684904,"school":true,"grade":8}]}'

Then I have this piece of code, that should give me the page I need:

j = session.get('https://interneturok.ru/school/student-journal/school/' + json.loads(grade.content)['pages']['grade'])

But I get this:

j = session.get('https://interneturok.ru/school/student-journal/school/' + json.loads(grade.content)['pages']['grade'])
TypeError: list indices must be integers or slices, not str

I tried a few other ways to do this:

j = session.get('https://interneturok.ru/school/student-journal/school/' + json.loads(grade.content)['grade'])

j = session.get('https://interneturok.ru/school/student-journal/school/' + json.loads(grade.content)['pages'][2])

But nothing works

Advertisement

Answer

You’re trying to retrieve a dictionary item with a list as a value. json.loads(grade.content)['pages'][0]['id'] should work.

x = json.loads(grade.content)['pages'][0]['id']
j = session.get('https://yoururl.com/' + str(x))

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement