-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp3.rb
More file actions
79 lines (68 loc) · 2.38 KB
/
app3.rb
File metadata and controls
79 lines (68 loc) · 2.38 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
require 'watir-webdriver'
require 'webdrivers'
require 'nokogiri'
require 'pry'
class Parser
def self.start
@vehiclelist = []
@carbrand = ARGV[0].downcase
@carmodel = ARGV[1].downcase
@browser = Watir::Browser.new
@browser.goto ENV['SITE1']
Parser.prepare_page
Parser.scrape_data
Parser.check_next_page
Parser.count_result
end
private
def self.count_result
puts "Total records: #{@vehiclelist.flatten.count}"
end
def self.scrape_data
htmlpage = Nokogiri::HTML.parse(@browser.html)
table = htmlpage.css('#old_cars_list')
@carname = table.css('tr > td[2]').map {|n| n.text.strip }
@carprice = table.css('tr > td[3]').map {|n| n.text.strip }
@caryear = table.css('tr > td[4]').map {|n| n.text.strip }
@cardmileage = table.css('tr > td[5]').map {|n| n.text.strip }
@carlink = table.css('tr > td[2] > a').map {|n| n['href'] }
export_data
sleep 1
end
def self.export_data
@data = [@carname, @carprice, @caryear, @cardmileage, @carlink ].transpose.map{|n,p,y,m,l| { name: n, price: p, year: y, mileage: m, link: l }}
@vehiclelist.push(@data)
sleep 1
end
def self.prepare_page
@browser.span(:id => 'pseudo_link_inventory_trade').click
select_brand = @browser.select_list(:id => 'car_brand_offer_trade_in')
@select_content_brand = select_brand.options.map(&:text).map(&:strip).map(&:downcase)
if @select_content_brand.index(@carbrand).nil?
puts "No such brand name found."
exit
end
brand_choose = @select_content_brand.index(@carbrand)
@browser.select_list(:id => 'car_brand_offer_trade_in').option(index: brand_choose).click
select_model = @browser.select_list(:id => 'car_model_offer_trade_in')
@select_model_content = select_model.options.map(&:text).map(&:strip).map(&:downcase)
if @select_model_content.index(@carmodel).nil?
puts "No such model name found."
exit
end
model_choose = @select_model_content.index(@carmodel)
@browser.select_list(:id => 'car_model_offer_trade_in').option(index: model_choose).click
@browser.button(:id => "big_input", :index => 3).click
sleep 1
end
def self.check_next_page
count = @browser.as(:class, "right").count
while count == 1
@browser.a(:class, "right").click
sleep 1
scrape_data
count = @browser.as(:class => "right").count
end
end
end
Parser.start