Send Ad Free SMS in India

Google Suggest + Ruby = Fun!

June 29, 2009 | In: Ruby, Web 2.0

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.

  1.  
  2. require "rubygems"
  3. require "open-uri"
  4. require "json"
  5. require "pp"
  6.  
  7. module Suggest
  8.  
  9.   def self.google(keywords)
  10.     keywords = keywords.join(" ")
  11.     url = "http://clients1.google.com/complete/search?hl=en&client=hp&q=#{keywords}&cp=13"
  12.     json_data = open(url).read.split("window.google.ac.h(")[-1][0..-2]
  13.     json = JSON::parse(json_data)
  14.     output = []
  15.     json[1].each do |data|
  16.       output << json[0].strip + " " + data.first.split("b>")[1].split("<!–")[0].strip
  17.     end
  18.     return output
  19.   end
  20.  
  21. end
  22.  
  23. pp Suggest::google([‘prepaid’])
  24.  

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 :)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Reddit Post to StumbleUpon

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Comment Form

blog comments powered by Disqus