baidu 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/baidu.rb +8 -35
- metadata +1 -1
data/lib/baidu.rb
CHANGED
@@ -106,47 +106,20 @@ class BaiduResult
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def ranks(host=nil)
|
109
|
-
|
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?}
|
109
|
+
@ranks ||= @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.split(/\s/).first)).host unless rank.nil?}
|
126
110
|
if host.nil?
|
127
111
|
@ranks
|
128
112
|
else
|
129
|
-
|
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
|
113
|
+
@ranks.each_with_index.map{|h,i| i if !h.nil? and h==host}.compact
|
139
114
|
end
|
140
115
|
end
|
141
116
|
|
142
|
-
#
|
117
|
+
#look up a word and get the rank of a uri with $host
|
143
118
|
def rank(host)#on base of ranks
|
144
|
-
ranks.
|
145
|
-
if
|
146
|
-
|
147
|
-
|
148
|
-
return id if line['host'] == host
|
149
|
-
end
|
119
|
+
ranks.each_with_index do |uri,index|
|
120
|
+
next if uri.nil?
|
121
|
+
return index+1 if uri.split('/')[0].strip == host
|
122
|
+
#return index+1 if URI.parse(URI.encode(uri)).host == host
|
150
123
|
end
|
151
124
|
return nil
|
152
125
|
end
|
@@ -162,4 +135,4 @@ class BaiduResult
|
|
162
135
|
def next
|
163
136
|
@page = BaiduResult.new(Mechanize.new.click(@page.link_with(:text=>/下一页/))) unless @page.link_with(:text=>/下一页/).nil?
|
164
137
|
end
|
165
|
-
end
|
138
|
+
end
|