Google Suggest – (as put by Google) use a wide range of information to predict the queries users are most likely to want to see. For example, Google Suggest uses data about the overall popularity of various searches to help rank the refinements it offers. An example of this type of popularity information can be found in the Google Zeitgeist.
So how can we harness this information using Ruby?
The answer is simple – with each character you type into the search box, google sends a HTTP GET request back to its servers, which respond back with a JSON packed reply containing the suggestions. Following ruby module parses it using JSON gem.
-
-
require "rubygems"
-
require "open-uri"
-
require "json"
-
require "pp"
-
-
module Suggest
-
-
def self.google(keywords)
-
keywords = keywords.join(" ")
-
url = "http://clients1.google.com/complete/search?hl=en&client=hp&q=#{keywords}&cp=13"
-
json_data = open(url).read.split("window.google.ac.h(")[-1][0..-2]
-
json = JSON::parse(json_data)
-
output = []
-
json[1].each do |data|
-
output << json[0].strip + " " + data.first.split("b>")[1].split("<!–")[0].strip
-
end
-
return output
-
end
-
-
end
-
-
pp Suggest::google([‘prepaid’])
-
Output…
makuchaku@FourFractions:~$ ruby ./lib/suggest.rb
["prepaid energy meter",
"prepaid mobile recharge",
"prepaid expenses",
"prepaid sbi",
"prepaid visa card",
"prepaid cards",
"prepaid visa",
"prepaid credit card",
"prepaid icici",
"prepaid airtel"]
Nice 10 minutes script it was
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
Comment Form