Library Data Exercises#
Exercise: Count Works per Creator #
In the JSON book data, each item has a creator
field located in metadata
.
Complete the code below step-by-step to count the number of items per creator.
You will need to browse the data.
a)#
As before, the results contains the items
in _embedded
.
Fetch these into a list items
as before. Then loop over the items and print each item. Each item is a large dictionary, so expect a lot of text.
b)#
Now, change the content of the for loop. Instead of just printing the item, get the list of creators. You will need to find the path to follow. For now, print the list of creators.
c)#
Instead of printing the list of creators, loop over the list and print each creator.
d)#
Now we need to check if the creator already exists in our lookup table/dictionary creator2count
.
Use an if statement to start the count at one, if the creator isn’t registered in the table.
e)#
Otherwise, if the creator is registered in the dictionary, increase the count by one.
f)#
With the counting completed, all that remains is to show the results.
Loop over creator2count
and print the result.
import requests
import json
URL = "https://api.nb.no/catalog/v1/items?digitalAccessibleOnly=true&size=20&filter=mediatype:bøker&q=Skatterett"
data = requests.get(URL).json()
creator2count = {} # example content {'Bing, Jon': 1}