Python Tutorial Exercise 3: Implement the govAPI_UK class
Write a
govAPI_UK implementation class which retrieves the following attributes from the UK government API:
-
family name
-
given ame
-
gender
-
party
-
id (by extracting the number out of the _about attributes)
Below you see a dedicated record of the
members API endpoint. You can access the viewer via the following link:
-
As one can see below, there are 4451 member records.
-
Via the “Output to” menu item, you can change the format for example from HTML to JSON.
-
The full API is described here:
-
Scroll down the full page, to get an overview of all attributes for the Members endpoint, as well as possibilities to query parametes
-
The following request will return a JSON data structure of all members, which you have to transform:
-
The JSON is shown below
-
This formatted view can be achieved by using the online json formatter: https://jsonformatter.curiousconcept.com/
Some programming hints:
-
When you have fetched the JSON and stored in a variable, i.e. loaded_json you have to iterate over the array found in
loaded_json[‘result’][‘items’] this is actually the members list -
As request parameter provide
-
pageSize=500 store this values as part of the config_UK.yaml file (refer to the govAPI_CH class implementation)
-
_page which you count up for each page you request (again refer to the govAPI_CH class)
-
For the loop condition you can use the attribute totalResults, which is provided as an element in the JSON document.
-
I.e. fetch “x” times 500 records until you retrieved overall 4451 records.
comments powered by Disqus