読者です 読者をやめる 読者になる 読者になる

宿泊予約サイトの情報をopen-uriで開いて、nokogiriで解析してみる。

Booking.com: 東京のホテル. 今すぐホテル予約!

require 'kconv'
require 'open-uri'
require 'nokogiri'

UserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'
doc = Nokogiri::HTML(open('http://www.booking.com/searchresults.ja.html?label=gen173nr-1DCAEoggJCAlhYSBViBW5vcmVmaHWIAQGYARW4AQfIAQzYAQPoAQH4AQKoAgM;sid=feb3e89b7a094231eeb4d815e4f6d9b7;dcid=12;checkin_monthday=03&checkin_year_month=2016-05&checkout_monthday=04&checkout_year_month=2016-05&dest_id=-246227&dest_type=city&from_history=1&group_adults=2&group_children=0&have_hh=1&no_rooms=1&si=ad&si=ai&si=ci&si=co&si=di&si=la&si=re&;link_search_history=1&tfl_cwh=1','User-Agent' => UserAgent))


File.open("scraping_hotels.csv", "w:utf-8", ){|f|
f.print "\uFEFF"
doc.css('.sr_item').each{|row|
  line_ary = []
  line_ary.push(row.css(".hotel_name_link").first.text)
  line_ary.push(row.css(".score_from_number_of_reviews").first.text)
  if row.css('.smart_price_style').length > 0 then
    line_ary.push(row.css(".smart_price_style").first.text)
  end
  f.puts line_ary.map{|e|"\"#{e.gsub("\n","")}\""}.join(",")
}
}