baidu 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/baidu.rb +35 -8
- metadata +1 -1
data/lib/baidu.rb
CHANGED
@@ -106,20 +106,47 @@ class BaiduResult
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def ranks(host=nil)
|
109
|
-
@ranks
|
109
|
+
return @ranks unless @ranks.nil?
|
110
|
+
@ranks = Hash.new
|
111
|
+
@page.search("//table[@class=\"result\"]").each do |table|
|
112
|
+
id = table['id']
|
113
|
+
@ranks[id] = Hash.new
|
114
|
+
url = @page.search("//table[@id=\"#{table['id']}\"]//span[@class=\"g\"]").first
|
115
|
+
a = @page.search("//table[@id=\"#{table['id']}\"]//h3/a")
|
116
|
+
@ranks[id]['text'] = a.text
|
117
|
+
@ranks[id]['href'] = a.first['href'].sub('http://www.baidu.com/link?url=','').strip
|
118
|
+
unless url.nil?
|
119
|
+
url = url.text.strip
|
120
|
+
@ranks[id]['host'] = URI(URI.encode("http://#{url}")).host
|
121
|
+
else
|
122
|
+
@ranks[id]['host'] = nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
#@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?}
|
110
126
|
if host.nil?
|
111
127
|
@ranks
|
112
128
|
else
|
113
|
-
|
129
|
+
host_ranks = Hash.new
|
130
|
+
@ranks.each do |id,line|
|
131
|
+
if host.class == Regexp
|
132
|
+
host_ranks[id] = line if line['host'] =~ host
|
133
|
+
elsif host.class == String
|
134
|
+
host_ranks[id] = line if line['host'] == host
|
135
|
+
end
|
136
|
+
end
|
137
|
+
host_ranks
|
138
|
+
#'not finished'#@ranks.each_with_index.map{|h,i| i if !h.nil? and h==host}.compact
|
114
139
|
end
|
115
140
|
end
|
116
141
|
|
117
|
-
#
|
142
|
+
#return the top rank number from @ranks with the input host
|
118
143
|
def rank(host)#on base of ranks
|
119
|
-
ranks.
|
120
|
-
|
121
|
-
|
122
|
-
|
144
|
+
ranks.each do |id,line|
|
145
|
+
if host.class == Regexp
|
146
|
+
return id if line['host'] =~ host
|
147
|
+
elsif host.class == String
|
148
|
+
return id if line['host'] == host
|
149
|
+
end
|
123
150
|
end
|
124
151
|
return nil
|
125
152
|
end
|
@@ -135,4 +162,4 @@ class BaiduResult
|
|
135
162
|
def next
|
136
163
|
@page = BaiduResult.new(Mechanize.new.click(@page.link_with(:text=>/下一页/))) unless @page.link_with(:text=>/下一页/).nil?
|
137
164
|
end
|
138
|
-
end
|
165
|
+
end
|