class MMonitor::Parser
Attributes
body[RW]
item[RW]
items[RW]
Public Class Methods
new(dom)
click to toggle source
# File lib/mmonitor/parser.rb, line 8 def initialize(dom) return nil if dom.nil? self.body = dom.at(css_path[:body]) self.items ||= {} process unless self.body.nil? end
Public Instance Methods
extra()
click to toggle source
# File lib/mmonitor/parser.rb, line 46 def extra # 拓展操作 end
get_html(url, params)
click to toggle source
# File lib/mmonitor/parser.rb, line 70 def get_html(url, params) Spider.get_html(url, params) end
list()
click to toggle source
# File lib/mmonitor/parser.rb, line 33 def list # 产品列表 products = [] if css_path[:list].is_a?(Array) css_path[:list].each do |li| dom = self.body.at(li) products += dom.css(css_path[:item]) unless dom.nil? end else products += self.body.at(css_path[:list]).css(css_path[:item]) end products end
page(num)
click to toggle source
# File lib/mmonitor/parser.rb, line 66 def page(num) num end
page_key()
click to toggle source
# File lib/mmonitor/parser.rb, line 62 def page_key 'page' end
pages()
click to toggle source
# File lib/mmonitor/parser.rb, line 50 def pages self.body.at(css_path[:pages]).text.to_i end
photo_url()
click to toggle source
# File lib/mmonitor/parser.rb, line 90 def photo_url # 产品图片 self.item.at(css_path[:photo_url])['src'] end
price()
click to toggle source
# File lib/mmonitor/parser.rb, line 94 def price # 产品售价 if css_path.has_key?(:price) squish( self.item.at(css_path[:price]).try(:text) ).to_f else nil end end
price_url()
click to toggle source
# File lib/mmonitor/parser.rb, line 110 def price_url # 产品售价图片(可选) if css_path.has_key?(:price_url) self.item.at(css_path[:price_url])['src'] else nil end end
process()
click to toggle source
# File lib/mmonitor/parser.rb, line 15 def process self.list.each do |item| self.item = item id = spu_id unless id.nil? self.items[id] = { provider: self.provider, title: title, sku_id: sku_id, photo_url: photo_url, tag_price: tag_price, price: price, price_url: price_url } end end end
sku_id()
click to toggle source
# File lib/mmonitor/parser.rb, line 78 def sku_id # 单品ID if css_path.has_key?(:sku_id) squish( self.item[css_path[:sku_id]] ) else nil end end
spu_id()
click to toggle source
# File lib/mmonitor/parser.rb, line 74 def spu_id # 产品ID squish( self.item[css_path[:spu_id]] ) end
tag_price()
click to toggle source
# File lib/mmonitor/parser.rb, line 102 def tag_price # 产品原价(可选) if css_path.has_key?(:tag_price) squish( self.item.at(css_path[:tag_price]).try(:text) ).to_f else nil end end
title()
click to toggle source
# File lib/mmonitor/parser.rb, line 86 def title # 产品标题 squish( self.item.at(css_path[:title]).try(:text) ) end
total()
click to toggle source
# File lib/mmonitor/parser.rb, line 54 def total if css_path.has_key?(:total) squish( self.body.at(css_path[:total]).try(:text) ) else nil end end
Private Instance Methods
pirce_from_ocr(price_url)
click to toggle source
无货产品,通过图片获取价格
# File lib/mmonitor/parser.rb, line 126 def pirce_from_ocr(price_url) str = MMonitor::Spider.get_ocr(price_url) str.match( /\d+\.\d+/ ).to_f end
squish(string)
click to toggle source
字符处理
# File lib/mmonitor/parser.rb, line 121 def squish(string) string.gsub(/¥|¥/, '').strip.gsub(/\s+/, '') rescue nil end