Class: Baiduserp::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/baiduserp/parser.rb

Defined Under Namespace

Modules: Ads_Right, Ads_Top, PinPaiZhuanQu, Ranks, Related_Keywords, Result_Num

Instance Method Summary collapse

Instance Method Details

#parse(html) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/baiduserp/parser.rb', line 11

def parse(html)
  @file = Hash.new
  @serp = Hash.new

  @file[:html] = html
  @file[:doc] = Nokogiri::HTML(html)

  self.class.constants.each do |m|
    #puts m
    eval "@serp[:#{m.downcase}] = #{m}.parse @file"
    #p @serp.keys
  end

  @serp
end

#parse_file(file_path) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/baiduserp/parser.rb', line 31

def parse_file(file_path)
  if File.exists? file_path
    html = open(file_path).read
  else
    html = Client.get(URI.escape(file_path)).body
  end
  html = html.encode!('UTF-8','UTF-8',:invalid => :replace)
  parse html
end

#search(keyword) ⇒ Object



27
28
29
# File 'lib/baiduserp/parser.rb', line 27

def search(keyword)
  parse_file("http://www.baidu.com/s?wd=#{keyword}")
end