Class: BaiduResult
- Inherits:
-
Object
- Object
- BaiduResult
- Defined in:
- lib/baidu.rb
Instance Method Summary collapse
- #how_many ⇒ Object
-
#initialize(page) ⇒ BaiduResult
constructor
A new instance of BaiduResult.
- #next ⇒ Object
-
#rank(host) ⇒ Object
return the top rank number from @ranks with the input host.
- #ranks(host = nil) ⇒ Object
- #related_keywords ⇒ Object
Constructor Details
#initialize(page) ⇒ BaiduResult
Returns a new instance of BaiduResult.
103 104 105 106 |
# File 'lib/baidu.rb', line 103 def initialize(page) raise ArgumentError 'should be Mechanize::Page' unless page.class == Mechanize::Page @page = page end |
Instance Method Details
#how_many ⇒ Object
154 155 156 |
# File 'lib/baidu.rb', line 154 def how_many @how_many ||= @page.search("//span[@class='nums']").map{|num|num.content.gsub(/\D/,'').to_i unless num.nil?}.first end |
#next ⇒ Object
162 163 164 |
# File 'lib/baidu.rb', line 162 def next @page = BaiduResult.new(Mechanize.new.click(@page.link_with(:text=>/下一页/))) unless @page.link_with(:text=>/下一页/).nil? end |
#rank(host) ⇒ Object
return the top rank number from @ranks with the input host
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/baidu.rb', line 143 def rank(host)#on base of ranks ranks.each do |id,line| if host.class == Regexp return id if line['host'] =~ host elsif host.class == String return id if line['host'] == host end end return nil end |
#ranks(host = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/baidu.rb', line 108 def ranks(host=nil) return @ranks unless @ranks.nil? @ranks = Hash.new @page.search("//table[@class=\"result\"]").each do |table| id = table['id'] @ranks[id] = Hash.new url = @page.search("//table[@id=\"#{table['id']}\"]//span[@class=\"g\"]").first a = @page.search("//table[@id=\"#{table['id']}\"]//h3/a") @ranks[id]['text'] = a.text @ranks[id]['href'] = a.first['href'].sub('http://www.baidu.com/link?url=','').strip unless url.nil? url = url.text.strip @ranks[id]['host'] = URI(URI.encode("http://#{url}")).host else @ranks[id]['host'] = nil end end #@page.search("//table[@class=\"result\"]").map{|table|@page.search("//table[@id=\"#{table['id']}\"]//span[@class=\"g\"]").first}.map{|rank|URI(URI.encode('http://'+rank.text.strip)).host unless rank.nil?} if host.nil? @ranks else host_ranks = Hash.new @ranks.each do |id,line| if host.class == Regexp host_ranks[id] = line if line['host'] =~ host elsif host.class == String host_ranks[id] = line if line['host'] == host end end host_ranks #'not finished'#@ranks.each_with_index.map{|h,i| i if !h.nil? and h==host}.compact end end |
#related_keywords ⇒ Object
158 159 160 |
# File 'lib/baidu.rb', line 158 def @related_keywords ||= @page.search("//div[@id=\"rs\"]//tr//a").map{|keyword| keyword.text} end |