-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_rivm.py
39 lines (30 loc) · 1.19 KB
/
fetch_rivm.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
import csv
import json
from bs4 import BeautifulSoup
from datetime import datetime
from io import StringIO
from urllib.request import urlopen, Request
today_dt = datetime.today()
data_url = 'https://www.rivm.nl/coronavirus-kaart-van-nederland'
rivm_request = Request(data_url)
rivm_request.add_header('Referer', 'https://www.rivm.nl/')
rivm_request.add_header('User-Agent', 'curl/7.58.0 (+https://doemee.codefor.nl/)')
with urlopen(rivm_request) as conn:
rivm_html = BeautifulSoup(conn, 'html.parser')
rivm_csv = rivm_html.find('div', dict(id='csvData'))
csv_buffer = StringIO(rivm_csv.text)
first_line = csv_buffer.readline() # pop the '\n' so DictReader grabs first line header
if first_line != '\n':
csv_buffer.seek(0) # reset buffer if the first line was not actually '\n'
csv_data_reader = csv.DictReader(csv_buffer, delimiter=';')
first_entry = True
json_filename = 'docs/data/{:%Y-%m-%d}.json'.format(today_dt)
with open(json_filename, 'w') as json_outfile:
json_outfile.write('[')
for row in csv_data_reader:
if not first_entry:
json_outfile.write(',')
else:
first_entry = False
json.dump(row, json_outfile)
json_outfile.write(']')