Class: Qunar::Rank

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/qunar/rank.rb

Instance Method Summary collapse

Constructor Details

#initialize(hotelId) ⇒ Rank

Returns a new instance of Rank.



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
# File 'lib/qunar/rank.rb', line 11

def initialize(hotelId)

  raise "id should be string" if !hotelId.instance_of?(String)
  Capybara.current_driver = :poltergeist   #selenium / webkit
  Capybara.default_selector = :xpath
  Capybara.app_host = 'http://hotel.qunar.com'
  page.driver.headers = { "User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50" }
  @HotelSEQ = hotelId                #hotelId is "cityurl_hotelcode"
  @cityurl = hotelId[0..hotelId.rindex("_",-1)-1]
  @hotelcode = hotelId[hotelId.rindex("_",-1)+1..hotelId.length]
  @uri = URI(File.join('/city', @cityurl, "dt-#{@hotelcode}"))
  visit @uri
  puts 'cp 2'
  sleep 30
  # k = 0
  all('//td[@class="book-ct"]//*[text()="展开报价"] | //td[@class="book-ct"]//*[text()="全部订完"]').each do |spage|
    puts 'cp 2-1'
    # k += 1
    # filePath = 'test' + k.to_s + '.html'
    # File.open('test.html','w') do |file|
    #   file.puts page.html
    # end
    spage.trigger('click')
    sleep 1
    puts 'cp 2-2'
  end
  puts 'cp 3'
  #@page = Capybara.current_session.driver.browser.page_source    #use when choose capybara driver selenium
  @page = Capybara.current_session.driver.browser.body    #use when choose capybara driver webkit
  puts 'cp 4'
  @hotel = Nokogiri::HTML @page
  
end

Instance Method Details

#roomsObject



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
# File 'lib/qunar/rank.rb', line 45

def rooms
  rooms = []
  # room
  puts 'cp 5'
  @hotel.search("//ul[@class='htl-type-list clrfix']/li").each do |li|
    #puts 'one roomtype found'
    room = {}
    type = li.search("span[@class='type-name']").first.text.strip
    room['room_type'] = type
    room['otas'] = []
    # ota

    li.search("div[@class='similar-type-agent-list']/div/div").each do |row|
      otas = {}

      ota = row.search("div[@class='profile-tit']").first.text.strip
      otas['ota'] = ota

      bookable = row.search("a[@class='btn-book']/span")
      unless bookable.empty?
        bookable == bookable.first.text.strip
        if bookable == '订完'
          bookable = false
        else
          bookable = true
        end
      else
        bookable = true
      end
      otas['bookable'] = bookable

      insurance = row.search("p[@class='dbt-ct']").first
      if insurance == nil
        insurance = false
      else
        insurance = true
      end
      otas['insurance'] = insurance

      coupon = row.search("span[@class='fan']/em[@class='pr']").first

      if coupon == nil
        coupon = 0
      else
        coupon = coupon.text.sub(/¥/,'').strip.to_i
      end

      otas['coupon'] = coupon

      reserve_detail = row.search("div[@class='clrfix order-detail']").first.text.strip
      otas['reserve_detail'] = reserve_detail

      prepay = row.search("span[@title='需要预先支付房款']").first
      if prepay == nil
        prepay = false
      else
        prepay = true
      end
      otas['prepay'] = prepay

      price = row.search("p[@class='final-price ']/b").first.text.strip
      otas['price'] = price.to_i

      neat_price = row.search("p[@class='count_pr']").first.children[1].to_s
      otas['neat_price'] = neat_price.to_i

      room['otas'] << otas
    end
    rooms << room
    
  end
  puts 'cp 6'
  return rooms
end