class MMonitor::Strategies::Yhd

www.yhd.com/ctg/s2/c0-0/b/a-s1-v0-p1-price-d0-f06-m1-rt0-pid-mid0-k%E6%AC%A7%E8%8E%B1%E9%9B%85/

Attributes

body[RW]
item[RW]
items[RW]
page[RW]
pages_count[RW]
url[RW]

Public Class Methods

new(dom, url) click to toggle source
# File lib/mmonitor/strategies/yhd.rb, line 9
def initialize(dom, url)
  self.body = dom.at(css_path[:body])
  self.items ||= {}
  self.url = url.sub('s2', 'searchPage')
  self.pages_count = self.body.at(css_path[:pages]).text.split('/')[1].to_i
  self.page = 1
  process
  get_ajax(true)
  next_page
end

Public Instance Methods

css_path() click to toggle source
# File lib/mmonitor/strategies/yhd.rb, line 25
def css_path
  {
    body:      '#plist',
    list:      '#itemSearchList',
    item:      'li.search_item > div.search_item_box',
    pages:     '#rankOpDiv > div > div.select_page_num', # .text.split('/')[1]
    spu_id:    'pmid',
    title:     'p.title > a',
    photo_url: 'a.search_prod_img > img', # 'data-ks-lazyload'
    price:     'div.pricebox.clearfix  > span:nth-child(1)'
  }
end
pages() click to toggle source

分页伪装

# File lib/mmonitor/strategies/yhd.rb, line 48
def pages
  1
end
photo_url() click to toggle source
# File lib/mmonitor/strategies/yhd.rb, line 42
def photo_url # 产品图片
  img = self.item.at(css_path[:photo_url])
  img['original'] || img['src']
end
provider() click to toggle source

输出的产品

# File lib/mmonitor/strategies/yhd.rb, line 21
def provider
  :yhd
end
spu_id() click to toggle source
# File lib/mmonitor/strategies/yhd.rb, line 38
def spu_id # 产品图片
  self.item.at(css_path[:title])[css_path[:spu_id]]
end

Private Instance Methods

get_ajax(more=false) click to toggle source

加载更多商品

# File lib/mmonitor/strategies/yhd.rb, line 78
def get_ajax(more=false)
     params = more ? more_params : {}
       json = MMonitor::Spider.get_json(self.url, params)
  if json.has_key?('value')
    html = json['value']
    unless html.nil?
      self.body = ::Nokogiri::HTML("<div id='itemSearchList'>#{html}</div>")
      # 执行
      self.process
    end
  end
end
more_params() click to toggle source
# File lib/mmonitor/strategies/yhd.rb, line 70
def more_params
  {
    'isGetMoreProducts' => 1,
    'moreProductsDefaultTemplate' => 0
  }
end
next_page() click to toggle source
# File lib/mmonitor/strategies/yhd.rb, line 54
def next_page
  puts "分页提示:#{self.page}/#{self.pages_count}"
  puts '_'*88
  if self.pages_count > self.page
    self.page += 1
    self.url.gsub!("-p#{self.page-1}-", "-p#{self.page}-")
    get_ajax # 执行
    get_ajax(true)
  else
    self.body = nil
    self.item = nil
    return nil
  end
  next_page
end