Last active
November 27, 2016 22:30
-
-
Save sammachin/ec92808750a3577a0d68 to your computer and use it in GitHub Desktop.
Fetch data from the ONS API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We need to specify our api key here to use in the request | |
apikey = "XXXXXX" | |
#Now we'll setup the base url for the ons API | |
baseurl = "http://data.ons.gov.uk/ons/api/data/dataset/" | |
# We need to specify the dataset we are after, in this case its just the split of gender | |
dataset = "QS104EW" | |
#And pass in some of our parameters | |
payload = {'apikey': apikey, #our API Key from earlier | |
'context': 'Census', # We're looking at the census data | |
'geog' : '2011WARDH', # Using the 2011 Administrative Hieracry | |
'dm/2011WARDH' : 'K04000001', # And the geography code here is for all of England and Wales | |
'totals' : 'false', # We're only looking at one area so we don't care about totals | |
'jsontype' : 'json-stat' } # And we need to specify that we want the JSON-STAT output | |
r = requests.get(baseurl+"/"+dataset+".json", params=payload) # Now we make the request | |
obj = json.loads(r.text) # we take the response data and parse it as JSON to load into an object. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment