Class: MMonitor::Parser
- Inherits:
-
Object
- Object
- MMonitor::Parser
- Defined in:
- lib/mmonitor/parser.rb
Overview
解析器,负责网页模板解析。
Direct Known Subclasses
Strategies::Amazon, Strategies::Jd, Strategies::Jumei, Strategies::Lefeng, Strategies::Suning, Strategies::Tmall, Strategies::Yhd, Strategies::Yixun
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#item ⇒ Object
Returns the value of attribute item.
-
#items ⇒ Object
Returns the value of attribute items.
Instance Method Summary collapse
-
#extra ⇒ Object
拓展操作.
- #get_html(url, params) ⇒ Object
-
#initialize(dom) ⇒ Parser
constructor
A new instance of Parser.
-
#list ⇒ Object
产品列表.
- #page(num) ⇒ Object
- #page_key ⇒ Object
- #pages ⇒ Object
-
#photo_url ⇒ Object
产品图片.
-
#price ⇒ Object
产品售价.
-
#price_url ⇒ Object
产品售价图片(可选).
- #process ⇒ Object
-
#sku_id ⇒ Object
单品ID.
-
#spu_id ⇒ Object
产品ID.
-
#tag_price ⇒ Object
产品原价(可选).
-
#title ⇒ Object
产品标题.
- #total ⇒ Object
Constructor Details
#initialize(dom) ⇒ Parser
Returns a new instance of Parser.
9 10 11 12 13 14 |
# File 'lib/mmonitor/parser.rb', line 9 def initialize(dom) return nil if dom.nil? self.body = dom.at(css_path[:body]) self.items ||= {} process unless self.body.nil? end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
7 8 9 |
# File 'lib/mmonitor/parser.rb', line 7 def body @body end |
#item ⇒ Object
Returns the value of attribute item.
7 8 9 |
# File 'lib/mmonitor/parser.rb', line 7 def item @item end |
#items ⇒ Object
Returns the value of attribute items.
7 8 9 |
# File 'lib/mmonitor/parser.rb', line 7 def items @items end |
Instance Method Details
#extra ⇒ Object
拓展操作
47 48 49 |
# File 'lib/mmonitor/parser.rb', line 47 def extra # 拓展操作 end |
#get_html(url, params) ⇒ Object
71 72 73 |
# File 'lib/mmonitor/parser.rb', line 71 def get_html(url, params) Spider.get_html(url, params) end |
#list ⇒ Object
产品列表
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mmonitor/parser.rb', line 34 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) ⇒ Object
67 68 69 |
# File 'lib/mmonitor/parser.rb', line 67 def page(num) num end |
#page_key ⇒ Object
63 64 65 |
# File 'lib/mmonitor/parser.rb', line 63 def page_key 'page' end |
#pages ⇒ Object
51 52 53 |
# File 'lib/mmonitor/parser.rb', line 51 def pages self.body.at(css_path[:pages]).text.to_i end |
#photo_url ⇒ Object
产品图片
91 92 93 |
# File 'lib/mmonitor/parser.rb', line 91 def photo_url # 产品图片 self.item.at(css_path[:photo_url])['src'] end |
#price ⇒ Object
产品售价
95 96 97 98 99 100 101 |
# File 'lib/mmonitor/parser.rb', line 95 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 ⇒ Object
产品售价图片(可选)
111 112 113 114 115 116 117 |
# File 'lib/mmonitor/parser.rb', line 111 def price_url # 产品售价图片(可选) if css_path.has_key?(:price_url) self.item.at(css_path[:price_url])['src'] else nil end end |
#process ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mmonitor/parser.rb', line 16 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 ⇒ Object
单品ID
79 80 81 82 83 84 85 |
# File 'lib/mmonitor/parser.rb', line 79 def sku_id # 单品ID if css_path.has_key?(:sku_id) squish( self.item[css_path[:sku_id]] ) else nil end end |
#spu_id ⇒ Object
产品ID
75 76 77 |
# File 'lib/mmonitor/parser.rb', line 75 def spu_id # 产品ID squish( self.item[css_path[:spu_id]] ) end |
#tag_price ⇒ Object
产品原价(可选)
103 104 105 106 107 108 109 |
# File 'lib/mmonitor/parser.rb', line 103 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 ⇒ Object
产品标题
87 88 89 |
# File 'lib/mmonitor/parser.rb', line 87 def title # 产品标题 squish( self.item.at(css_path[:title]).try(:text) ) end |
#total ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/mmonitor/parser.rb', line 55 def total if css_path.has_key?(:total) squish( self.body.at(css_path[:total]).try(:text) ) else nil end end |