Class: ESpider::Front::Dianping::Hotel::Detail

Inherits:
Object
  • Object
show all
Defined in:
lib/espider/front/dianping/hotel/detail.rb

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Detail

Returns a new instance of Detail.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/espider/front/dianping/hotel/detail.rb', line 6

def initialize(id)
    @host = 'www.dianping.com'
    @id = id
    @current_url = URI::HTTP.build(
      :host => @host,
      :path => "/shop/#{id}"
    ).to_s
    @options = {
      :headers => {
          "User-Agent" => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36'
      }
    }
    @hotel = Nokogiri::HTML(HTTParty.get(@current_url,@options))
    @js = @hotel.search("//script[@class='J_auto-load']").map{|script|script.inner_html}.join("\n")
    @js = Nokogiri::HTML(@js)
end

Instance Method Details

#commentsObject



73
74
75
# File 'lib/espider/front/dianping/hotel/detail.rb', line 73

def comments
   @hotel.search("//div[@class='J_brief-cont']").map{|comment|comment.text.strip}
end

#facilitiesObject

return example:

“tags”=>[“国内长途电话”, “国际长途电话”, “拖鞋”, “雨伞”, “书桌”, “24小时热水”, “电热水壶”, “咖啡壶/茶壶”, “免费洗漱用品(6样以上)”, “免费瓶装水”, “迷你吧”, “小冰箱”, “浴衣”, “多种规格电源插座”, “110V电压插座”, “浴缸”, “独立淋浴间”, “吹风机”, “房内保险箱”, “中央空调”], “tags”=>[“棋牌室”, “室内游泳池”, “健身室”, “按摩室”, “桑拿浴室”, “足浴”, “SPA”, “烧烤”], “tags”=>[“中餐厅”, “西餐厅”, “酒吧”, “前台贵重物品保险柜”, “免费停车场”, “有可无线上网的公共区域”, “大堂吧”, “电梯”, “有可无线上网的公共区域 免费”], “tags”=>[“会议厅”, “商务中心”, “外币兑换服务”, “旅游票务服务”, “洗衣服务”, “送餐服务”, “专职行李员”, “行李寄存”, “叫醒服务”, “接机服务”]


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/espider/front/dianping/hotel/detail.rb', line 33

def facilities
    return @facilities if @facilities
    @facilities = []
    @js.search("//div[@class='introd-box']//li").each do |li|
        facility = {}
        title = li.search("span[@class='tit']").first
        next unless title
        facility['title'] = title.text
        facility['tags'] = []
        li.search("span[@class='introd-tag']").each do |tag|
            facility['tags'] << tag.text
        end
        @facilities << facility
    end
    @facilities
end

#imagesObject

return example:

href="http://www.dianping.com/photos/38858830">www.dianping.com/photos/38858830”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858827">www.dianping.com/photos/38858827”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858820">www.dianping.com/photos/38858820”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858806">www.dianping.com/photos/38858806”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858804">www.dianping.com/photos/38858804”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858801">www.dianping.com/photos/38858801”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858799">www.dianping.com/photos/38858799”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858782">www.dianping.com/photos/38858782”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858781">www.dianping.com/photos/38858781”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858777">www.dianping.com/photos/38858777”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858769">www.dianping.com/photos/38858769”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858762">www.dianping.com/photos/38858762”, :text=>“大堂”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858758">www.dianping.com/photos/38858758”, :text=>“门面”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858752">www.dianping.com/photos/38858752”, :text=>“大堂”, :small_pic=>“”, :big_pic=>“”, href="http://www.dianping.com/photos/38858748">www.dianping.com/photos/38858748”, :text=>“大堂”, :small_pic=>“”, :big_pic=>“


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/espider/front/dianping/hotel/detail.rb', line 52

def images
    url = URI::HTTP.build(
      :host => @host,
      :path => "/shop/#@id/photos"
    )
    images = Nokogiri::HTML(HTTParty.get(url.to_s,@options))
    results = []
    images.search("//li[@class='J_list']").each do |image|
        href = image.search("div/a").first['href']
        text = image.search("h3/a").text
        thumb = image.search("img").first['src']
        results << {
            :url => URI.join(@current_url,href).to_s,
            :text => text,
            :small_pic => thumb,
            :big_pic => thumb.sub(/\(.+\)/,'(700x700)')
        }
    end
    results
end

#introObject



27
28
29
# File 'lib/espider/front/dianping/hotel/detail.rb', line 27

def intro
    @js.search("//div[@class='intro-txt J_hotel-expand']/span[@class='Hide']").text
end

#nameObject



23
24
25
# File 'lib/espider/front/dianping/hotel/detail.rb', line 23

def name
    @hotel.search("//h1").first.text
end