-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_school_water_testing.py
executable file
·168 lines (105 loc) · 4.18 KB
/
parse_school_water_testing.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
import csv
import json
import requests
from bs4 import BeautifulSoup
class WaterTestSet():
"""
Contains set of water test results (e.g., all dps schools' water tests) to csv.
"""
field_names = [ 'school_name', 'status', 'url', 'fix_plan_status', 'fix_plan_url' ]
def __init__(self):
self.tests = []
def add(self, test_info):
self.tests.append(test_info)
def write(self, filename):
with open(filename, 'w', newline='') as csvfile:
# writer = csv.writer(filename, delimiter=',', quotechar='"')
writer = csv.DictWriter(csvfile, fieldnames=self.field_names, quoting=csv.QUOTE_ALL)
writer.writeheader()
for test in self.tests:
row_data = { item[0] : item[1] for item in zip(self.field_names, test.get_data()) }
writer.writerow(row_data)
def clean_val(val):
return val.strip()
def clean_href(href):
path = href.replace('/Portals/0/docs/Schools/School Water Testing/', '')
href = 'http://detroitmi.theneighborhoods.org/sites/detroitmi.localhost/files/migrated_docs/school-water-testing/' + path
return href.replace(' ', '%20')
class WaterTestInfo():
"""
An individual set of water test results for a given school.
"""
def __init__(self, row):
cols = row.find_all('td')
name_col = cols[0]
href_col = cols[1]
status_col = cols[2]
if len(cols) < 4:
mitigation_plan_col = None
else:
mitigation_plan_col = cols[3]
self.name = clean_val(name_col.text)
if href_col.find('a'):
href = href_col.find('a').get('href')
self.path = clean_href(href)
else:
self.path = ''
self.status = clean_val(status_col.find('strong').text)
self.fix_plan_path = ''
self.fix_plan_status = ''
if self.status == 'Elevated' and mitigation_plan_col:
child = mitigation_plan_col.findChild()
if child:
href = child.get('href')
if href:
self.fix_plan_path = clean_href(href)
self.fix_plan_status = clean_val(child.text)
else:
self.fix_plan_status = clean_val(mitigation_plan_col.text)
def get_data(self):
return [ self.name, self.status, self.path, self.fix_plan_status, self.fix_plan_path ]
def is_complete(self):
# name and status always required
if not self.name and self.status:
return False
# sometimes the report is not available if the location is 'Pending'
if not self.path:
return self.status == 'Pending'
return True
def __str__(self):
return self.name
def get_pages():
urls = {
"dps": "http://www.detroitmi.gov/Government/Departments-and-Agencies/Detroit-Health-Department/School-Water-Testing/DPS-Water-Testing",
"day_care": "http://www.detroitmi.gov/Government/Departments-and-Agencies/Detroit-Health-Department/School-Water-Testing/Day-Care-Water-Testing",
"charter": "http://www.detroitmi.gov/Government/Departments-and-Agencies/Detroit-Health-Department/School-Water-Testing/Charter-and-EAA-Water-Testing",
}
pages = {}
for key, url in urls.items():
response = requests.get(url)
pages[key] = response.text
return pages
def parse_pages(pages):
test_sets = {}
for key, page in pages.items():
test_set = WaterTestSet()
soup = BeautifulSoup(page, 'html.parser')
divs = soup.find_all('div', 'det-school-water')
for div in divs:
rows = div.find_all('tr')
header_parsed = False
for row in rows:
if header_parsed:
test_info = WaterTestInfo(row)
test_set.add(test_info)
else:
header_parsed = True
test_sets[key] = test_set
return test_sets
if __name__ == "__main__":
pages = get_pages()
test_sets = parse_pages(pages)
for key, test_set in test_sets.items():
filename = key + '.csv'
test_set.write(filename=filename)