-
Notifications
You must be signed in to change notification settings - Fork 2
/
bios_getter.py
217 lines (162 loc) · 6.19 KB
/
bios_getter.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
206
207
208
209
210
211
212
213
214
#-*-coding: utf8-*-
'''
$ python bios_getter.py
Get and parse death penalty bio pages
from Texas Dept of Criminal Justice website.
Writes data to stdout as CSV, so pipe it into desired location.
Joe Nudell, 2013
'''
from bs4 import BeautifulSoup as bs
from nameparser import HumanName
from sys import argv, stderr, stdout, exit
from urlparse import urljoin
import os
import re
import urllib
import urllib2
import csv
source_url = "http://www.tdcj.state.tx.us/death_row/dr_executed_offenders.html"
headers = [
'Date of execution',
'Execution #',
'Age (when executed)',
'First Name',
'Middle Name',
'Last Name',
'Name Suffix',
'TDCJ Number',
'Date of Birth',
'Date Received',
'Age (when received)',
'Education Level (highest grade completed)',
'Date of Offense',
'Age (at time of offense)',
'County',
'Race',
'Gender',
'Hair Color',
'Height',
'Weight',
'Eye Color',
'Native County',
'Native State',
'Prior Occupation',
'Prior Prison Record',
'Summary of Incident',
'Co-Defendants',
'Race and Gender of Victim',
'Headshot (base-64)',
'statement',
'Link to Offender Information',
'Link to Last Statement'
]
if __name__=='__main__':
# Get the main index page
print >>stderr, "Scraping Texas Dept. of Criminal Justice - Death Row Info"
print >>stderr, "\n\n"
print >>stderr, "Downloading main index ... ",
uh_main = urllib2.urlopen(source_url)
soup_index = bs(uh_main.read())
index_table = soup_index.find('table')
print >>stderr, "Done.\n"
print >>stderr, "Beginning scraping.\n"
# Find all rows on the page, skip first because it's headers
rows = index_table.find_all('tr')[1:]
max_ = len(rows)
out_file = csv.writer(stdout)
out_file.writerow(headers)
for i, row in enumerate(rows):
# Iterate over table rows, store info, grab links, download links
print >>stderr, "Reading row", i+1, "out of", max_, "..."
entry = []
cells = row.find_all('td')
# First - date of execution. In cell #7
entry.append(cells[7].text.encode('utf8'))
# Second - Execution #. Cell #0.
entry.append(cells[0].text.encode('utf8'))
# Third - Age when executed. In Cell #6.
entry.append(cells[6].text.encode('utf8'))
# Get links
bio_link = urljoin(source_url, cells[1].find('a')['href'])
statement_link = urljoin(source_url, cells[2].find('a')['href'])
last_name = cells[3].text.encode('utf8')
first_name = cells[4].text.encode('utf8')
_name = first_name + " " + last_name
# --- Download BIO link ---
print >>stderr, " Downloading bio for", _name, " ...",
yay_have_data = False
if bio_link.endswith('.html'):
# Bio available in HTML! Parse it.
uh = urllib2.urlopen(bio_link)
soup_bio = bs(uh.read())
print >>stderr, "Done."
print >>stderr, " Parsing bio ...",
main_table = soup_bio.find('table',
attrs={'class':'tabledata_deathrow_table'})
if main_table:
# Data is available!
yay_have_data = True
line_tmp = [tr.find('td',
attrs={'class':'tabledata_align_left_deathrow'})\
.text.encode('utf8')
for tr in main_table.find_all('tr')]
name = HumanName(line_tmp[0])
line = [name.first, name.middle, name.last, name.suffix] \
+ line_tmp[1:]
try:
image_link = urljoin(bio_link,
main_table.find('img')['src'])
except Exception as e:
print >>stderr, "[Error: image not available.", str(e),"]"
image_link = None
supp_info = []
for p in soup_bio.find_all('p')[1:]:
# This one is a problematic / ill-formed field.
try:
supp_info.append(p.find('span')\
.find_next('br').next_sibling.encode('utf8'))
except Exception as e:
print >>stderr, "[Error:", str(e), ", p=", str(p), "]",
supp_info.append("")
while len(supp_info)<5:
# Make sure supplemental info is five cells in length
supp_info.append("Not Available")
line += supp_info
if image_link:
# download image
print >>stderr, "fetching headshot ...",
uhimg = urllib2.urlopen(image_link)
img = urllib.quote(uhimg.read().encode('base64'))
else:
img = "Not Available"
line.append(img)
entry += line
print >>stderr, "Done."
if not yay_have_data:
# Frown, no data
# Get what's known from index page
print >>stderr, "Can't get bio. Falling back to index info ...",
entry += [first_name,"",last_name,"",cells[5].text.encode('utf8')]
entry += ["Not Available"]*6
entry += [c.text.encode('utf8') for c in [cells[9], cells[8]]]
entry += ["Not Available"]*13
print >>stderr, "Done."
# -- Download STATEMENT link ---
print >>stderr, " Downloading statement ...",
uh = urllib2.urlopen(statement_link)
soup_statement = bs(uh.read())
print >>stderr, "Done."
print >>stderr, " Parsing statement ...",
_title = soup_statement.find('p',text=re.compile(".*statement.*",re.I))
statement = "Unknown"
if _title:
s = _title.find_next('p')
if s:
statement = s.text.encode('utf8')
entry.append(statement)
print >>stderr, "Done."
print >>stderr, " Printing info ...",
entry = [c.strip() for c in entry]
entry += [bio_link, statement_link]
out_file.writerow(entry)
print >>stderr, "All done!"