-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
57 lines (43 loc) · 1015 Bytes
/
scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# %%
# #!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Purpose
Explore gov.uk organisations API
Inputs
- Web: gov.uk organisations API
Outputs
- None
Parameters
None
Notes
None
'''
import json
import requests
# %%
# Prepare to call API
url_stub = 'https://www.gov.uk/api/organisations?page='
headers = {'accept': 'application/json'}
def call_api(url_stub, page_number, headers):
url = url_stub + str(page_number)
r = requests.get(url, headers=headers)
if r.ok:
record_list.extend(r.json()['results'])
try:
if r.json()['next_page_url']:
page_number += 1
call_api(url_stub, page_number, headers)
except KeyError:
pass
return
# %%
# Call API
page_number = 1
record_list = []
call_api(url_stub, page_number, headers)
# %%
# Save output
with open('organisations.json', 'w') as f:
json.dump(record_list, f)
# %%