Class: Qunar::Rank
Instance Method Summary collapse
-
#citynameCn ⇒ Object
Get the Chinese name of the City.
-
#cityNameEn ⇒ Object
Get the English name of the City.
-
#hotelInfo ⇒ Object
Get the Hotel details.
-
#html ⇒ Object
Html file.
-
#initialize(hotelId) ⇒ Rank
constructor
A new instance of Rank.
-
#name ⇒ Object
Get the Chinese name of the hotel.
-
#rooms ⇒ Object
Get the rooms details.
-
#screen_shot ⇒ Object
Get the screen_shot.
-
#star ⇒ Object
Get the level of the hotel.
Constructor Details
#initialize(hotelId) ⇒ Rank
Returns a new instance of Rank.
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 |
# File 'lib/qunar/rank.rb', line 5 def initialize(hotelId) raise "id should be string" if !hotelId.instance_of?(String) .current_driver = :webkit # :selenium/:poltergeist .default_selector = :xpath .app_host = 'http://hotel.qunar.com' # Header for webkit page.driver.header 'User-Agent', "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.77.4 (KHTML, like Gecko) Version/7.0.5 Safari/537.77.4" @HotelSEQ = hotelId #hotelId is "cityurl_hotelcode" @cityurl, suffix, @hotelcode = hotelId.rpartition('_') @uri = URI(File.join('/city', @cityurl, "dt-#{@hotelcode}")) page.visit @uri limiter = 0 begin find("//td[@class='book-ct']//*[text()='展开报价']") page.all('//td[@class="book-ct"]//*[text()="展开报价"] | //td[@class="book-ct"]//*[text()="全部订完"]').each do |spage| puts 'cp 2-1' spage.click puts 'cp 2-2' end puts 'cp 3' # Expand hidden otas page.all("//div[@class='more-quote js-room-more']/a").each do |link| link.click end rescue =>e limiter+=1 retry if limiter<3 raise e end #@page = Capybara.current_session.driver.browser.page_source #use when choose capybara driver selenium @hotel = Nokogiri::HTML page.html #use when choose capybara driver webkit end |
Instance Method Details
#citynameCn ⇒ Object
Get the Chinese name of the City
162 163 164 |
# File 'lib/qunar/rank.rb', line 162 def citynameCn page.evaluate_script("(function f() { return cityName; }());") end |
#cityNameEn ⇒ Object
Get the English name of the City
167 168 169 |
# File 'lib/qunar/rank.rb', line 167 def cityNameEn page.evaluate_script("(function f() { return cityNameEn; }());") end |
#hotelInfo ⇒ Object
Get the Hotel details
172 173 174 175 176 177 |
# File 'lib/qunar/rank.rb', line 172 def hotelInfo { :hotel_id => @HotelSEQ, :hotel_name => self.name, :city => self.cityNameEn, :city_cn => self.citynameCn, :star => self.star } end |
#html ⇒ Object
Html file
185 186 187 |
# File 'lib/qunar/rank.rb', line 185 def html @hotel end |
#name ⇒ Object
Get the Chinese name of the hotel
130 131 132 133 134 135 136 137 138 |
# File 'lib/qunar/rank.rb', line 130 def name name = @hotel.search("//div[@class='htl-info fr']//span") if name.empty? name = '该酒店不存在' else name = name.first.text.strip end return name end |
#rooms ⇒ Object
Get the rooms details
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 |
# File 'lib/qunar/rank.rb', line 43 def rooms rooms = [] # room @hotel.search("//li[@class='default-type defaultpricetype similar-expand' or @class='default-type defaultpricetype similar-expand b_result_item_on']").each do |li| #puts 'one roomtype found' room = {} type = li.search("span[@class='type-name js-p-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 ota = ota.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("a[@class='js-guarantee-plan']").first if insurance == nil insurance = false else insurance = true end otas[:safe] = insurance guarantee = row.search("cite[text()='需担保']").first if guarantee == nil guarantee = false else guarantee = true end otas[:guarantee] = guarantee 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 reduce = row.search("span[@class='fan lapse']/em[@class='pr']").first if reduce == nil reduce = 0 else reduce = reduce.text.sub(/¥/,'').strip.to_i end otas[:reduce] = reduce 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 neat_price = row.search("p[@class='count_pr']").first.children[1].to_s otas[:original_price] = price.to_i otas[:final_price] = neat_price.to_i room[:otas] << otas end rooms << room end return rooms end |
#screen_shot ⇒ Object
Get the screen_shot
180 181 182 |
# File 'lib/qunar/rank.rb', line 180 def screen_shot page.save_screenshot("#{Time.now}.png") end |
#star ⇒ Object
Get the level of the hotel
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/qunar/rank.rb', line 141 def star name_tag = @hotel.search('//div[@class="htl-info fr"]//em[@class="dangci"]').first return 0 if name_tag.nil? if name_tag.text.include?'经济型' 1 elsif name_tag.text.include?'舒适型' 3 elsif name_tag.text.include?'高档型' 4 elsif name_tag.text.include?'豪华型' 5 elsif name_tag.text.include?'star20' 2 elsif name_tag.text.include?'star50' 5 else 0 end end |