-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
205 lines (160 loc) · 5.2 KB
/
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re
import requests
import sys
from bs4 import BeautifulSoup
import geocoder
from pprint import pprint
import json
INSPECTION_DOMAIN = 'http://info.kingcounty.gov'
INSPECTION_PATH = '/health/ehs/foodsafety/inspections/Results.aspx?'
INSPECTION_PARAMS = {
'Output': 'W',
'Business_Name': '',
'Business_Address': '',
'Longitude': '',
'Latitude': '',
'City': '',
'Zip_Code': '',
'Inspection_Type': 'All',
'Inspection_Start': '',
'Inspection_End': '',
'Inspection_Closed_Business': 'A',
'Violation_Points': '',
'Violation_Red_Points': '',
'Violation_Descr': '',
'Fuzzy_Search': 'N',
'Sort': 'H'
}
def get_inspection_page(**kwargs):
inspection_params = INSPECTION_PARAMS.copy()
for k, v in kwargs.iteritems():
if k in INSPECTION_PARAMS:
inspection_params[k] = v
search = INSPECTION_DOMAIN + INSPECTION_PATH
response = requests.get(search, params=inspection_params)
response.raise_for_status()
return response.content, response.encoding
def write_inspection_page(search_results):
with open('inspection_page.html', 'w') as page:
page.write(search_results)
def load_inspection_page(filename):
with open(filename, 'r') as page:
return page.read()
def parse_source(source_content, source_encoding):
soup = BeautifulSoup(
source_content,
'html5lib',
from_encoding=source_encoding
)
return soup
def extract_data_listings(html):
id_finder = re.compile(r'PR[\d]+~')
return html.find_all('div', id=id_finder)
def has_two_tds(element):
verdict = False
if element.name == 'tr':
children = element.findChildren('td')
if len(children) == 2:
verdict = True
return verdict
def clean_data(cell):
cell = cell.text
return cell.strip(' \n:-')
def extract_restaurant_metadata(listing):
metadata_rows = listing.find('tbody').find_all(
has_two_tds, recursive=False
)
rdata = {}
current_label = ''
for row in metadata_rows:
key_cell, val_cell = row.find_all('td', recursive=False)
new_label = clean_data(key_cell)
current_label = new_label if new_label else current_label
rdata.setdefault(current_label, []).append(clean_data(val_cell))
return rdata
def is_inspection_row(element):
verdict = False
if element.name == 'tr':
children = element.findChildren('td')
if len(children) == 4:
firstchild = clean_data(children[0]).lower()
if ('inspection' in firstchild
and firstchild.split()[0] != 'inspection'):
verdict = True
return verdict
def extract_score_data(listing):
inspection_rows = listing.find_all(is_inspection_row)
samples = len(inspection_rows)
total = high_score = average = 0
for row in inspection_rows:
strval = clean_data(row.find_all('td')[2])
try:
intval = int(strval)
except (ValueError, TypeError):
samples -= 1
else:
total += intval
high_score = intval if intval > high_score else high_score
if samples:
average = total / float(samples)
data = {
u'Average Score': average,
u'High Score': high_score,
u'Total Inspections': samples
}
return data
def generate_results(test=False, count=10):
kwargs = {
'Zip_Code': '98112',
'Inspection_Start': '01/01/2015',
'Inspection_End': '07/01/2015'
}
if test:
html = load_inspection_page('inspection_page.html')
encoding = 'utf-8'
else:
html, encoding = get_inspection_page(**kwargs)
document = parse_source(html, 'utf-8')
listings = extract_data_listings(document)
for listing in listings[:count]:
metadata = extract_restaurant_metadata(listing)
score_data = extract_score_data(listing)
score_data.update(metadata)
yield score_data
def get_geojson(search_result):
address = ' '.join(search_result.get('Address', ''))
if not address:
return None
response = geocoder.google(address)
geoj = response.geojson
desired_keys = (
'Business Name',
'Address',
'Average Score',
'High Score',
'Total Inspections',
)
inspection_data = {}
for key, val in search_result.items():
if key in desired_keys:
if isinstance(val, list):
val = ' '.join(val)
inspection_data[key] = val
new_addr = geoj['properties'].get('addresss')
if new_addr is not None:
inspection_data['Address'] = new_addr
geoj['properties'] = inspection_data
return geoj
if __name__ == '__main__':
test = len(sys.argv) > 1 and sys.argv[1] == 'test'
total_result = {'type': 'FeatureCollection', 'features': []}
for result in generate_results(test=True):
geo_result = get_geojson(result)
total_result['features'].append(geo_result)
pprint(sorted(total_result['features'],
key=lambda v: v['properties']['Average Score'])
)
with open('my_map.json', 'w') as fh:
json.dump(total_result, fh)