-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
182 lines (154 loc) · 6.24 KB
/
scrape.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
"""
Written by Steve Birstok (http://www.stevebirstok.com)
"""
### IMPORTS ###
# regular expressions
import re
# os path functions
import os
# md5 sums
import md5
# Network IO
import urllib2
# URL Parser
from urlparse import urlparse
# BeautifulSoup for Scraping
from bs4 import BeautifulSoup
### END IMPORTS ###
### VARIABLES ###
# URLs
stateURL = 'http://www.dc.state.fl.us/ActiveInmates/'
inmatePopulationSearchURL = stateURL + 'list.asp?DataAction=Filter&SexOffOnly=0&dcnumber=&LastName=&FirstName=&SearchAliases=on&Sex=M&Race=ALL&OffenseCategory=ALL&ClassificationStatus=ALL&CurrentCustody=ALL&IdentifierType=ALL&Identifier=&EyeColor=ALL&HairColor=ALL&FromAge=&ToAge=&FromWeight=&ToWeight=&FromHeightFeet=&FromHeightInches=&ToHeight=&ToHeightFeet=&ToHeightInches=&ZipCode=&ScarType=ALL&ScarLocation=ALL&ScarDescription=&photosonly=on&nophotos=on&items=50&CommitmentCounty=ALL&subjecttype=ALL&facility=ALL&workskill=ALL'
### END VARIABLES ###
# classes, functions
class OffenderScraper:
def GetURL(self, url):
"""Gets a URL's content and caches it using md5 of URL string"""
# Parse URL for '//' (e.g.: 'http://')
urlschema = urlparse(url)
if urlschema.netloc == '':
url = 'http://' + urlschema.path
else:
url = urlschema.geturl()
content = ""
# Caching based on md5's of URL's
# Create Cache Folder
cacheDir = '/tmp/OffenderScrape/'
if not os.path.exists(cacheDir):
# create if not exist
os.makedirs(cacheDir)
cacheFileName = md5.new(url).hexdigest()
try:
# try to open cache
cache = open(cacheDir + cacheFileName, 'r+b')
cacheContents = cache.read()
if(len(cacheContents) > 0):
# cache is not blank
content = cacheContents
else:
# cache is blank, fill it.
response = urllib2.urlopen(url)
content = response.read()
cache.write(content)
cache.close()
except IOError:
# cache does not exist, create it.
cache = open(cacheDir + cacheFileName, 'w+b')
response = urllib2.urlopen(url)
content = response.read()
cache.write(content)
cache.close()
return content
def ScrapeTableLinks(self, htmlContent):
"""Scrapes Tables for Links to Detail pages"""
soup = BeautifulSoup(htmlContent, 'lxml')
contentContainer = soup.find(id="dcCSScontentContainer")
table = contentContainer.find_all('table')[2]
ths = table.find_all('th')
headers = []
for item in ths:
headers.append(item.get_text())
HTMLlinks = table.find_all('a')
links = []
for link in HTMLlinks:
# Only parse links that lead to an offender detail page.
if "detail.asp" in link.get('href'):
if not link.get('href') in links:
links.append(link.get('href'))
return links
def GetInmateDetails(self, detailContent, onlyShowRecords=False):
"""Scrapes Offender Detail Page"""
soup = BeautifulSoup(detailContent, 'lxml')
contentContainer = soup.find(id="dcCSScontentContainer")
if onlyShowRecords == True:
# Use regular expressions to see how many records this page reports there are
matches = re.search("Record: ([\d\-,]+) of ([\d\-,]+)", str(contentContainer))
return matches.group(2).strip().replace(',', '')
inmate = {}
mugshot = contentContainer.find('img', {'alt': 'Offender Picture'})
if mugshot and len(mugshot) > 0:
inmate['mugshot'] = stateURL + mugshot.get('src').strip('/')
inmateInfoHeaders = contentContainer.find_all('td', {'width': '40%'})
inmateInfoInfo = contentContainer.find_all('td', {'width': '60%'})
i = 0
info = 0
while i < len(inmateInfoHeaders):
try:
inmate[inmateInfoHeaders[i].get_text().strip().replace(':', '').replace(' ', '_').lower()] = inmateInfoInfo[i].get_text().strip()
info = info + 1
except:
pass
i = i + 1
# table = contentContainer.find_all('table')
# '''
# index - desc
# 0 - (This information was current as of ...)
# 1 - (This information was current as of ...)
# 2 - Inmate Data
# 3 - Mugshot
# 4 - More Inmate Data
# '''
# print table
# break
if info > 0:
return inmate
return None
def GenDetailLink(self, bookmark, sessionID):
return 'detail.asp?Bookmark=' + str(bookmark) + '&From=list&SessionID=' + str(sessionID)
# Main App Function
class Main():
def __init__(self):
print '[.] Loading Offenderscraper Library.'
DataScraper = OffenderScraper()
print 'Loading Offenders'
# ScrapeTableLinks
DetailPageLinks = DataScraper.ScrapeTableLinks(DataScraper.GetURL(inmatePopulationSearchURL))
content = DataScraper.GetURL(stateURL + DetailPageLinks[0])
if len(content) < 0:
print 'No content on first offender page.'
exit()
# Get Total Records from first Offendor page
TotalRecords = DataScraper.GetInmateDetails(content, True)
# Initiate the array to store our offendors
inmates = []
# Append first offender
inmates.append(DataScraper.GetInmateDetails(content))
# Get Detail Link Structure
matches = re.search('SessionID=(\d+)', DetailPageLinks[0])
session_id = matches.group(1)
print '[!] Entering loop! Processing ' + str(TotalRecords) + ' records.'
i = 1
while i <= TotalRecords:
content = DataScraper.GetURL(stateURL + DataScraper.GenDetailLink(i, session_id))
if len(content) < 0:
print 'No content on bookmark ' + str(i)
exit()
inmate = DataScraper.GetInmateDetails(content)
inmates.append(inmate)
i = i + 1
print str(len(inmates)) + ' inmates loaded.'
# for inmate in inmates:
# print inmate
return None
# execute
Main()