Created
February 9, 2019 16:16
-
-
Save RobinL/9fc189d22777e6f7a9968d5ce5adf950 to your computer and use it in GitHub Desktop.
Filter api example
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 requests | |
import json | |
import pandas as pd | |
from io import StringIO | |
import time | |
url = "https://api.beta.ons.gov.uk/v1/filters?submitted=true" | |
post = { | |
"dataset": { | |
"id": "mid-year-pop-est", | |
"edition": "time-series", | |
"version": 4 | |
}, | |
"dimensions": [ | |
{ | |
"name": "geography", | |
"options": [ | |
"E06000002" | |
] | |
} | |
] | |
} | |
post_data = json.dumps(post) | |
r = requests.post(url = url, data = post_data) #works | |
filter_meta = json.loads(r.text) | |
csv_location = filter_meta["links"]["filter_output"]["href"] | |
output_url = filter_meta['links']['filter_output']['href'] | |
r = requests.get(output_url) | |
filter_output_meta = json.loads(r.text) | |
csv_location = filter_output_meta['downloads']['csv']['href'] | |
time.sleep(1) | |
for i in range(100): | |
r = requests.get(csv_location) | |
df = pd.read_csv(StringIO(text)) | |
if text != "resource not found\n": | |
break | |
# Occasionally the 'downloads' key of the filter output comes back blank like so: | |
# {'dataset': {'edition': 'time-series', 'id': 'mid-year-pop-est', 'version': 4}, | |
# 'dimensions': [{'name': 'geography', 'options': ['E06000002']}], | |
# 'downloads': {'csv': {}, 'xls': {}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment