forked from openelections/openelections-data-il
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
217 lines (184 loc) · 6.6 KB
/
Copy pathscraper.py
File metadata and controls
217 lines (184 loc) · 6.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
import tabula
import pandas as pd
from decorator import decorator
import re
def scrape_tables(pdf, areas, county, pages=1, headers=None, labels=None):
def get_area(left,top,width,height):
y1 = top
x1 = left
y2 = top + height
x2 = left + width
return (y1,x1,y2,x2)
HEADER_AREA = get_area(**areas['header'])
LABEL_AREA = get_area(**areas['label'])
CONTENT_AREA = get_area(**areas['content'])
def get_header(pdf, pages):
if pages in headers:
df = pd.DataFrame(columns=range(len(headers[pages])))
df.loc[0] = headers[pages]
else:
df = tabula.read_pdf(pdf, area=HEADER_AREA, pages=pages, guess=False, pandas_options={'header': None})
header = list(df.fillna('').agg(lambda col: ' '.join(col)).str.strip().values)
header = [val for val in header if '%' not in val]
return [unicode('Precinct')] + header
def get_label(pdf, pages):
if pages in labels:
return labels[pages]
else:
df = tabula.read_pdf(pdf, area=LABEL_AREA, pages=pages, guess=False, latice=True, pandas_options={'header': None})
return df.loc[0].str.cat(sep=' ').strip()
@decorator
def drop_nan(f, *args, **kw):
df = f(*args, **kw)
for col in df:
if pd.isnull(df[col]).all():
df = df.drop(col, axis=1)
return df
@decorator
def drop_percent(f, *args, **kw):
grab_numeric = lambda series: series.str.split(expand=True)[0]
df = f(*args, **kw)
for col in df:
try:
if df[col].str.endswith('%').any():
# Sometimes the count and percent don't get split, so we grab the numeric col
if df[col].str.contains(' ').any():
df[col] = grab_numeric(df[col])
else:
df = df.drop(col, axis=1)
except AttributeError: # except isn't string
pass
return df
@decorator
def add_header(f, *args, **kw):
header = get_header(*args, **kw)
df = f(*args, **kw)
i = 0
while len(header) < len(df.columns):
header += ['Unnamed: %s' % i]
i += 1
display(header)
df.columns = header
return df
@decorator
def add_county(f, *args, **kw):
df = f(*args, **kw)
df['County'] = county
return df
@decorator
def drop_jurisdiction_header(f, *args, **kw):
df = f(*args, **kw)
df = df[df.Precinct != 'Jurisdiction Wide']
return df
@drop_jurisdiction_header
@add_county
@add_header
@drop_percent
@drop_nan
def get_content(pdf, pages):
df = tabula.read_pdf(pdf, pages=pages, pandas_options={'header': None})
return df
results = {}
for n in pages:
df = get_content(pdf, n)
display(df)
label = get_label(pdf, n)
if label not in results:
results[label] = df
# elif results[label].columns.all() == df.columns.all():
# results[label] = pd.concat([results[label], df])
else:
results[label] = pd.concat([results[label], df], join="outer")
print("Added result from page %s. Race: %s" % (n, label))
return results
def clean_results(results):
def lookup_office(label):
patterns = {
'D[0-9]+ REPRESENTATIVE': 'General Assembly',
'REPRES [0-9]+[A-Z]{2} DIST': 'General Assembly',
'D[0-9]+ CONGRESS': 'U.S. House',
'REP IN CONG [0-9]+[A-Z]{2} DIST': 'U.S. House',
'[0-9]+[A-Z]{2} CONGRESSIONAL DIST': 'U.S. House',
'D[0-9]+ STATE SENATE': 'State Senate',
'SENATE DIST [0-9]+': 'State Senate',
'TURN OUT': 'Voters',
'PRESIDENT': 'President'
}
for p in patterns:
if re.match(p, label):
return patterns[p]
return label.title()
def lookup_district(label):
try:
return re.search('[0-9]+', label).group(0)
except AttributeError:
return '-'
def lookup_candidate(cand):
return re.search('[A-Za-z \.-]+', cand).group(0).strip()
def lookup_party(cand):
try:
return re.search('(?<=\()([A-Z]+)', cand).group(0)
except AttributeError:
return '-'
def process_df(label, df):
template = pd.DataFrame(columns=['county', 'precinct', 'office', 'district', 'candidate', 'party', 'votes'])
template.precinct = df.Precinct.str.title()
df = df.drop('Precinct', axis=1)
template.county = df.County
df = df.drop('County', axis=1)
template.office = lookup_office(label)
template.district = lookup_district(label)
candidates = [val for val in df.columns.values if val not in ['Reg. Voters', 'Times Counted', 'Total Votes', 'Vote For', 'Times Over Voted', 'Number Of Under Votes']]
if lookup_office(label) == 'Voters':
candidates = ['Reg. Voters', 'Cards Cast']
def process_candidate(c):
c_df = template.copy()
c_df.votes = df[c]
c_df.candidate = lookup_candidate(c)
c_df.party = lookup_party(c)
c_df = c_df[~c_df.precinct.isin(['Presidential Ballot', 'Total', 'Elections Office'])]
c_df = c_df[c_df.votes >= 0]
return c_df
frames = [process_candidate(c) for c in candidates]
return pd.concat(frames)
final_frames = [process_df(label, df) for label,df in results.items()]
final = pd.concat(final_frames)
return final
def main(config, output):
results = scrape_tables(**config)
final = clean_results(results)
final.to_csv(output, index=False)
print "Wrote cleaned results to %s" % output
# SAMPLE CONFIG TO LOAD INTO SCRAPER:
#
# CONFIG = {
# 'areas': {
# 'label': {
# 'left': 37.56,
# 'top': 136,
# 'width': 527.65,
# 'height': 30
# },
# 'header': {
# 'left': 37.56,
# 'top': 157,
# 'width': 527.65,
# 'height': 50
# },
# 'content': {
# 'left': 34.89,
# 'top': 137.18,
# 'width': 527.65,
# 'height': 640.11
# }
# },
# "pdf": "../raw/2012+nov+6+il+edgar.pdf",
# "pages": [1,3,4,5,6,7,8,9,10],
# "county": "Edgar",
# 'headers': {
# 1: ['Reg. Voters', 'Cards Cast', '% Turnout'],
# },
# 'labels': {
# 1: 'TURN OUT',
# }
# }