/
scraper.py
354 lines (336 loc) · 12.6 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# This is a template for a Python scraper on morph.io (https://morph.io)
# including some code snippets below that you should find helpful
#sudo service morph restart
import scraperwiki
import lxml
import urlparse
import urllib2
#import mechanize
import requests
import lxml.html
import sqlite3
import time
import sys
sys.setrecursionlimit(10000)
#counties = ['adair','alfalfa','atoka','beaver','beckham','blaine','bryan','caddo','canadian','carter','cherokee','choctaw','cimarron','cleveland','coal','comanche','cotton','craig','creek','bristow','drumright','custer','delaware','dewey','ellis','garfield','garvin','grady','grant','greer','harmon','harper','haskell','hughes','jackson','jefferson','johnston','kay','poncacity','kingfisher','kiowa','latimer','leflore','lincoln','logan','love','major','marshall','mayes','mcclain','mccurtain','mcintosh','murray','muskogee','noble','nowata','okfuskee','oklahoma','okmulgee','henryetta','osage','ottawa','payne','pawnee','pittsburg','pontotoc','pottawatomie','pushmataha','rogermills','rogers','seminole','sequoyah','stephens','texas','tillman','tulsa','wagoner','washington','washita','woods','woodward']
#counties = ['oklahoma','tulsa','payne','garfield','canadian','cleveland','comanche','rogers']
counties = ['oklahoma']
#next_link = 0
#years = ['2011','2012','2013','2014','2015','2016','2017']
years = ['2016','2017']
CrimeSeverity = ['CF']
def CaseEndingNumbers():
for x in range(10042, 10043):
yield '%d' % x
def GetOklahomaStateCases():
for county in counties:
for CaseEndingNumber in ListOfCaseEndingNumbers:
for year in years:
for severity in CrimeSeverity:
yield 'http://www.oscn.net/dockets/GetCaseInformation.aspx?db=%s&number=%s-%s-%s' % (county, severity, year, CaseEndingNumber)
def scrape_table(root):
#create a record to hold the data
record = {}
#grab all table rows <tr> in table class="tblSearchResults"
rows = root.cssselect("table.caseStyle tr")
#for each row, loop through this
for row in rows:
#create a list of all cells <td> in that row
table_cells = row.cssselect("td")
if table_cells:
#if there is a cell, record the contents in our dataset, the first cell [0] in 'recipient' and so on
Case_Style = table_cells[0].text_content()
#print Case_Style
record['Case Style'] = table_cells[0].text_content()
record['Date Filed and Judge'] = table_cells[1].text_content()
#table_cellsurls = table_cells[0].cssselect("a")
#record['URL'] = table_cellsurls[0].attrib.get('href')
#record['Case Number'] = table_cells[0].strong.text_content()
#this line adds 1 to the ID no. we set at 0 earlier
#idno=idno+1
#record['ID'] = idno
print record, '------------'
counts = root.cssselect("div.CountsContainer")
countstotal = len(counts)
print "total number of counts:", countstotal
#countsrange = range(0, countstotal+1)
#for count in countsrange:
id=0
for count in counts:
id+=1
if counts:
record['Count'+str(id)] = count.text_content()
'''rows = count.cssselect('div.CountsContainer tr')
if rows:
id = 0
#rowstotal = len(rows)
#rowsrange = range(0,rowstotal+1)
#create a record to hold the data
#record = {}
#for each row, loop through this
#for rownum in rowsrange:
for row in rows:
id + 1
record["'Count'+str(id)"] = row[1].text_content()
#record['Count'+str(id)+'as disposed:'] = row[2].text_content()
#print "scraping row", rownum
#create a list of all cells <td> in that row
table_cells = row.cssselect("td")
if table_cells:
#print table_cells
record['Charges'] = table_cells[0].text_content()
record['Count Description:'] = table_cells[1].text_content()
record['Outcome:'] = table_cells[-1].text_content()
print record, '------------'
# Save the record to the datastore - 'ID' is our unique key - '''
print 'ALL DATA:', record
scraperwiki.sqlite.save(unique_keys=['Date Filed and Judge'], data=record)
def scrape_and_look_for_next_link(url):
#html = scraperwiki.scrape(url)
headers = { 'User-Agent' : 'Lucia Walinchus, Lucia@ProfessionalNewsServices.com, 646-397-7761' }
page = requests.get(url, headers=headers)
#req = urllib2.Request(url, None, headers)
#html = urllib2.urlopen(req).read()
#page = requests.get(url)
html = page.content
#print html
root = lxml.html.fromstring(html)
scrape_table(root)
#CaseEndingNumber += 1
global i
i = (i + 1)
if i < 451:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 451:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 451 < i < 901:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 901:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 901 < i < 1351:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 1351:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 1351 < i < 1801:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 1801:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 1801 < i < 2251:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 2251:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 2251 < i < 2701:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 2701:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 2701 < i < 3151:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 3151:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 3151 < i < 3601:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 3601:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 3601 < i < 4051:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 4051:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 4051 < i < 4501:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 4501:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 4501 < i < 4951:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 4951:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 4951 < i < 5401:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 5401:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 5401 < i < 5851:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if i == 5851:
time.sleep(65)
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
if 5851 < i < 6301:
next_url = ListofOKCases[i]
print next_url
record = {}
#record['URL'] = next_url
#scraperwiki.sqlite.save(['URL'], record)
scrape_and_look_for_next_link(next_url)
# ---------------------------------------------------------------------------
# START HERE: define your starting URL - then
# call a function to scrape the first page in the series.
# ---------------------------------------------------------------------------
base_url = 'http://www.oscn.net/dockets/'
starting_url = urlparse.urljoin(base_url, 'GetCaseInformation.aspx?db=garfield&number=CF-2011-1')
print starting_url
global i
i = 1
#for i in range(0,1):
#There are 743 cases but 468 appears to be the server request limit
CaseEndingNumbers()
ListOfCaseEndingNumbers = list(CaseEndingNumbers())
GetOklahomaStateCases()
ListofOKCases = list(GetOklahomaStateCases())
scrape_and_look_for_next_link(starting_url)
# # Read in a page
# html = scraperwiki.scrape("http://foo.com")
#
# # Find something on the page using css selectors
# root = lxml.html.fromstring(html)
# root.cssselect("div[align='left']")
#
# # Write out to the sqlite database using scraperwiki library
# scraperwiki.sqlite.save(unique_keys=['name'], data={"name": "susan", "occupation": "software developer"})
#
# # An arbitrary query against the database
# scraperwiki.sql.select("* from data where 'name'='peter'")
# You don't have to do things with the ScraperWiki and lxml libraries.
# You can use whatever libraries you want: https://morph.io/documentation/python
# All that matters is that your final data is written to an SQLite database
# called "data.sqlite" in the current working directory which has at least a table
# called "data".