so, i want to make python script to get all list value of "id" in this JSON.
And here the script i use
import requests
from requests.structures import CaseInsensitiveDict
url = "https://traderie.com/api/adopt_me/listings?selling=true&auction=false&page=0&seller=495259712&completed=false&active=all"
headers = CaseInsensitiveDict()
headers["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
headers["Authorization"] = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjQ5NTI1OTcxMiIsInVzZXJuYW1lIjoiQ2xvdWR5LlNoZWlsYTAzIiwiZnJpZW5kX2NvZGUiOm51bGwsInBob25lIjpudWxsLCJkaXNjb3JkIjpudWxsLCJvdGhlciI6bnVsbCwiY3JlYXRlZF9hdCI6IjIwMjItMDEtMjBUMTM6MzY6MDIuODE4WiIsInVwZGF0ZWRfYXQiOiIyMDIyLTAxLTIwVDEzOjM2OjAyLjgxOFoiLCJkaXNjb3JkX2lkIjpudWxsLCJ2aWxsYWdlcl9uYW1lIjpudWxsLCJpc2xhbmRfbmFtZSI6bnVsbCwic3RhdHVzIjoib25saW5lIiwidGltZXpvbmUiOm51bGwsImlwIjoiNTEuNzkuMTYxLjI0MiwgMTcyLjcwLjE4OC42IiwiYmlvIjpudWxsLCJiYW5uZWQiOmZhbHNlLCJlbWFpbCI6InNoYXJrNzQzLjJ5ei40N0BnbWFpbC5jb20iLCJ0d2l0dGVyIjpudWxsLCJ0d2l0dGVyX2lkIjpudWxsLCJkaXNjb3JkX25vdGlmcyI6ZmFsc2UsIndoaWNoX21vZF9iYW5uZWQiOm51bGwsImxhbmd1YWdlcyI6bnVsbCwidHVybmlwX2V4Y2hhbmdlIjpudWxsLCJoaWRlX2NvbnRhY3RzIjpudWxsLCJyZWZlcnJlZF9pZCI6bnVsbCwiZW1haWxfbWFya2V0aW5nIjp0cnVlLCJwcm9maWxlX2ltZyI6bnVsbCwiaW5mb18xIjpudWxsLCJpbmZvXzIiOm51bGwsImluZm9fMyI6bnVsbCwicm9ibG94X2lkIjpudWxsLCJyb2Jsb3hfdXNlcm5hbWUiOm51bGwsImJhbm5lZF9nYW1lIjpudWxsLCJzaWdudXBfbWV0aG9kIjoiZW1haWwiLCJyZXF1ZXN0X2RtIjp0cnVlLCJhcHBsZV9pZCI6bnVsbCwiZ29vZ2xlX2lkIjpudWxsLCJub3RlcyI6bnVsbCwidHdpdGNoIjpudWxsLCJ5b3V0dWJlIjpudWxsLCJpbnN0YWdyYW0iOm51bGwsInRpa3RvayI6bnVsbCwiYmFuX3JlYXNvbiI6bnVsbCwibm90aWZpY2F0aW9uX3NldHRpbmdzIjpudWxsLCJzdHJpcGVfY3VzdG9tZXJfaWQiOm51bGwsInN0cmlwZV9zdWJzY3JpcHRpb25fZW5kIjpudWxsLCJzdHJpcGVfc3Vic2NyaXB0aW9uX3N0YXR1cyI6bnVsbCwic3RyaXBlX2NhbmNlbF9hdCI6bnVsbCwicm9sZXMiOm51bGwsImlhdCI6MTY0MjY4NTc5MX0.v_EjZ8Wipjr6CnwnMQgQuKrV4IUs-zYLHqvARpsPQwo"
resp = requests.get(url, headers=headers)
json = resp.json()
listings = json["listings"]
print(listings["id"])
output
Traceback (most recent call last): File "main.py", line 14, in print(listings["id"]) TypeError: list indices must be integers or slices, not str
CodePudding user response:
This is because listings is a list of dictionaries, not a dictionary. You can use:
listings[0]["id"]
to get the ID of the first listing.
Alternatively, a simple list comprehension gets all IDs:
listing_ids = [listing["id"] for listing in listings]
Which outputs (for example)
['132982950', '844572039', '1154301185', '1825769295', '749762050', '1015551895', '1914007952', '653876816', '1851808554', '884374799', '1475828358', '1143158393', '67794054', '1944788346', '75060683', '639374907', '1729840421', '182868664', '746987097', '1695237351', '1963349071', '731335050', '1998602300', '1454789034', '894446787', '1310523772', '1343425709', '252474315', '790239965', '1169168224', '77072073', '782777064', '1733251429', '76484476', '577511992', '1548295745', '217148692', '778264112', '1192636205', '1084575147', '94264930', '1759842313', '1635270495', '281968428', '1586757783', '934885503', '1993886816', '415912889', '1445742823', '394209616']
CodePudding user response:
first of all: json["listings"] is a list
second of all: you must iterate over the list:
listings = json["listings"]
for listing in listings:
print(listing["id"])
done
