'Error adding a key value pair to the dictionary of artists
The API of the Artsy project provides information about some artists, their works, exhibitions.
I'm trying to make a get request to all artists by their id from a file dataset_24476_4.txt
Example of a string from a file
4d8b92a44eb68a1b2c000328
An example of the answer can be found at the link
The python interpreter throws the error name = r['name'] KeyError: 'name'
, but print(r["name"])
works correctly
# -*- coding: utf-8 -*-
import requests
client_id = '---'
client_secret = '---'
r = requests.post("https://api.artsy.net/api/tokens/xapp_token",
data = {
"client_id": client_id,
"client_secret": client_secret
}
).json()
token = r["token"]
headers = {
"X-Xapp-Token" : token
}
artists = {}
# test get request
r = requests.get(
f"https://api.artsy.net/api/artists/4d8b92b34eb68a1b2c0003f4",
headers=headers
).json()
print(r)
with open('dataset_24476_4.txt') as file:
for num in file:
num = num[:-1]
r = requests.get(
f"https://api.artsy.net/api/artists/{num}",
headers=headers
).json()
name = r['name']
birthday = r['birthday']
print(name, birthday)
artists.update({
name : birthday,
})
print(sorted(
artists.items(),
key=lambda x: (
x[1],
x[0]
)
))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|