Reputation: 635
I have this json api: https://api.glive.live/h5/v1/live_room/home?&page_size=1000
How do I print all "anchor_id"'s ?
import requests
api = "https://api.glive.live/h5/v1/live_room/home?"
url = api + '&page_size=1000'
raw_json_data = requests.get(url).json()
data = raw_json_data['rooms']['anchor_id']
print(data)
This gives me output:
data = raw_json_data['rooms']['anchor_id']
TypeError: list indices must be integers or slices, not str
I then try to do:
data = raw_json_data['rooms'](str['anchor_id'])
And get output:
data = raw_json_data['rooms'](str['anchor_id'])
TypeError: 'type' object is not subscriptable
Upvotes: 0
Views: 367
Reputation: 81684
You are getting the error because raw_json_data['rooms']
returns a list of dictionaries, and indexing into a list with a string (['anchor_id']
) does not make any sense as the original error suggests.
The way you tried to solve it (raw_json_data['rooms'](str['anchor_id'])
) does not make any sense due to many reasons, the most obvious one is the fact that str
is a type, hence str[...]
is a TypeError
.
Since raw_json_data['rooms']
returns a list, you have to iterate over it with a loop:
for room in raw_json_data['rooms']:
print(room['anchor_id'])
Upvotes: 1