-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintelliscraper.rb
More file actions
479 lines (306 loc) · 8.46 KB
/
intelliscraper.rb
File metadata and controls
479 lines (306 loc) · 8.46 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
require 'HTTParty'
require 'Pry'
require 'Nokogiri'
require 'csv'
require 'JSON'
require 'mechanize'
vin_numbers = []
input_file_name = []
results = []
final_results = []
i = 0;
#Ask the user for the input file name
puts "\n"
puts "***************** VINSCRAPER 1.0 *****************\n"
puts "--------------------------------------------------\n"
puts " Motorcycle Injury Prevention Institute \n"
puts " Center for Urban Transportation Research \n"
puts "--------------------------------------------------\n"
puts "\n"
puts "--------------------------------------------------\n"
puts "Developed for @Dr.Chanyoung Lee by @Shahzada Azam\n"
puts "--------------------------------------------------\n"
puts "\n"
puts "Please place your input file in CSV format in the current working directory."
puts "The VIN numbers must be listed int he first column starting from the first row. Any header rows/labels must be deleted."
puts "\n"
print "- Please enter input file name (without extension): \n"
input_file_name = gets().chomp
input_file_name = input_file_name + ".csv"
puts "Your input file is " + input_file_name
if File.file?(input_file_name) && File.exist?(input_file_name)
print "- Reading file......\n"
csv_file = File.read(input_file_name)
csv = CSV.parse(csv_file, :headers => false)
csv.each do |row|
vin_numbers.push(row[0])
end
#Test printing out un-sanitized VINs
# vin_numbers.each do |row|
# puts row
# end
#Sanitizing VIN numbers by removing / - . and anything other than numbers and alphabets
vin_numbers.each do |row|
# puts "printing row"
row.to_s.gsub!(/[^0-9A-Za-z]/, '')
# puts row
end
# p vin_numbers //for printing out VINs
puts "There are " + vin_numbers.count.to_s + " VIN entries in the file."
print "- Starting scraping......\n"
CSV.open('results.csv', 'w') do |csv|
while i <= vin_numbers.count
vin_number_input = vin_numbers[i]
agent = Mechanize.new
page = agent.get('http://www.iihs-hldi.org/MotovinDirect.asp?ccr=EHS7N5F2F2L5&scr')
form = page.form('frmVIN')
form.txtVIN = vin_numbers[i]
i = i + 1;
page = agent.submit(form)
page.search("td").each do |item|
results.push(item.text.strip)
end
# vin = results[6]
# hldi_class_name = results[20]
#Code for VIN Output
count = 0
index = 0
for j in results
if j == "Output VIN"
index = count
end
count = count + 1
end
vin_number_output = results[index+1]
# puts "****** Output VIN *******"
# puts "Output VIN: " + vin_number_output
#Code for Check Digit
count = 0
index = 0
for j in results
if j == "Check Digit"
index = count
end
count = count + 1
end
check_digit = results[index+1]
# puts "****** Check Digit *******"
# puts "Check Digit: " + check_digit
#Code for Model Year
count = 0
index = 0
for j in results
if j == "Model"
index = count
end
count = count + 1
end
model_year = results[index+1]
# puts "****** Model Year *******"
# puts "Model Year: " + model_year
#Code for Make
count = 0
index = 0
for j in results
if j == "Model"
index = count
end
count = count + 1
end
make = results[index+2]
# puts "****** Make *******"
# puts "Make: " + make
#Code for Series
count = 0
index = 0
for j in results
if j == "Model"
index = count
end
count = count + 1
end
series = results[index+3]
# puts "****** Series *******"
# puts "Series: " + series
#Code for Model
count = 0
index = 0
for j in results
if j == "Model"
index = count
end
count = count + 1
end
model = results[index+4]
# puts "****** Model *******"
# puts "Model: " + model
#Code for HLDI class name
count = 0
index = 0
for j in results
if j == "Displacement"
index = count
end
count = count + 1
end
hldi_class_name = results[index-1]
# puts "****** HlDI Class Name *******"
# puts "HLDI Class name: " + hldi_class_name
#Code for Displacement
count = 0
index = 0
for j in results
if j == "Displacement"
index = count
end
count = count + 1
end
displacement = results[index+1]
# puts "****** Displacement *******"
# puts "Displacement: " + displacement
#Code for Price
count = 0
index = 0
for j in results
if j == "Price"
index = count
end
count = count + 1
end
price = results[index+1]
# puts "****** Price *******"
# puts "Price: " + price
#Code for Weight
count = 0
index = 0
for j in results
if j == "Weight"
index = count
end
count = count + 1
end
weight = results[index+1]
# puts "****** Weight *******"
# puts "Weight: " + weight
#Code for Wheelbase
count = 0
index = 0
for j in results
if j == "Wheelbase"
index = count
end
count = count + 1
end
wheelbase = results[index+1]
# puts "****** Wheelbase *******"
# puts "Wheelbase: " + wheelbase
#Code for ABS
count = 0
index = 0
for j in results
if j == "ABS"
index = count
end
count = count + 1
end
abs = results[index+1]
# puts "****** ABS *******"
# puts "ABS: " + abs
#Code for Transmission
count = 0
index = 0
for j in results
if j == "Transmission"
index = count
end
count = count + 1
end
transmission = results[index+1]
# puts "****** Transmission *******"
# puts "Transmission: " + transmission
#Code for Drive
count = 0
index = 0
for j in results
if j == "Transmission"
index = count
end
count = count + 1
end
drive = results[index+3]
# puts "****** Drive *******"
# puts "Drive: " + drive
#Code for Engine Type
count = 0
index = 0
for j in results
if j == "HorsePower"
index = count
end
count = count + 1
end
engine_type = results[index+1]
# puts "****** Engine Type *******"
# puts "Engine Type: " + engine_type
#Code for Engine Drive
count = 0
index = 0
for j in results
if j == "HorsePower"
index = count
end
count = count + 1
end
engine_drive = results[index+2]
# puts "****** Engine Drive *******"
# puts "Engine Drive: " + engine_drive
#Code for Torque
count = 0
index = 0
for j in results
if j == "Torque"
index = count
end
count = count + 1
end
torque = results[index+4]
# puts "****** Torque *******"
# puts "Torque: " + torque
#Code for HorsePower
count = 0
index = 0
for j in results
if j == "HorsePower"
index = count
end
count = count + 1
end
horsepower = results[index+4]
# puts "****** HorsePower *******"
# puts "HorsePower: " + horsepower
final_results.push(vin_number_input,vin_number_output, check_digit, model_year, make, series, model, hldi_class_name, displacement, price, weight, wheelbase, abs, transmission, drive, engine_type, engine_drive, torque, horsepower)
# puts results
#Sanitizing final_results
for j in final_results
if j == "ENTER MOTORCYCLE VIN"
j.to_s.gsub!(/(ENTER MOTORCYCLE VIN)/, '')
end
end
for k in final_results
k.to_s.gsub!(/[^0-9A-Za-z]/, '')
end
# final_results.gsub!(/[^ENTER MOTORCYCLE VIN]/, '')
puts ""
puts final_results
puts ""
csv << final_results
results.clear
final_results.clear
end
end
puts "Scraping complete!"
else
print " File " + input_file_name + " doesn't exist in the current directory\n"
print " Please place file in current directory and run this script again\n"
return
end