Created
December 4, 2017 13:20
-
-
Save rmania/67b88074a93ed9fd4e81870401dda601 to your computer and use it in GitHub Desktop.
Call the BAG API #AmsterdamCityData
This file contains hidden or 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
import pprint | |
import requests | |
import json | |
pc_list = ['1069KJ', '1058NH', '1057PV'] | |
# or 4-digit postal codes | |
pc_list = ['1069', '1058', '1057'] | |
num_url = 'https://api.data.amsterdam.nl/bag/nummeraanduiding/' | |
# totaal resultaten opvangen in results list.. | |
results = [] | |
for pc_code in pc_list: | |
print('loading %s' % pc_code) | |
params = {'postcode': pc_code, 'detailed' :1} | |
response = requests.get(num_url, params) | |
data = response.json() | |
# LET OP meerdere nummeraanduiding per pc dus ook meerdere buurten | |
# mogelijk. Grenzen lopen niet mooi.. | |
result = {'postcode': pc_code} | |
if not data.get('results'): | |
results.append(result) | |
# nothing found. | |
continue | |
eerste_hit = data['results'][0] | |
if not eerste_hit: | |
print('ERROR %s', pc_code) | |
def visdata(*args): | |
""" | |
Given arguments get data from nested field | |
like item[arg1][agr2][argx].. but without crashing. | |
""" | |
nextvalue = eerste_hit | |
for arg in args[:-1]: | |
nextvalue = nextvalue.get(arg, {}) | |
if not nextvalue: | |
return '' | |
return nextvalue.get(args[-1], '') | |
# indicate what to extract | |
result['buurt'] = visdata('buurt', '_display') | |
result['sd'] = visdata('gebiedsgerichtwerken', 'naam') | |
result['sd_code'] = visdata('gebiedsgerichtwerken', 'code') | |
result['buurt_combi'] = visdata('buurtcombinatie', 'naam') | |
result['voll_code']= visdata('buurtcombinatie', 'vollcode') | |
# voeg postcode resultaat toe aan totaal resultaten lijst. | |
results.append(result) | |
#pprint.pprint(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment