Skip to content

Instantly share code, notes, and snippets.

@ryanjones
Created June 10, 2012 18:53
Show Gist options
  • Save ryanjones/2906912 to your computer and use it in GitHub Desktop.
Save ryanjones/2906912 to your computer and use it in GitHub Desktop.
Parse out the hrefs from a google search
require 'open-uri'
require 'nokogiri'
def google_results(keyword)
begin
results = []
doc = Nokogiri::HTML(open("https://www.google.com/search?q=#{keyword}"))
doc.xpath("//h3//a[@href]").each do |href|
puts href.attributes["href"].value.match(/(http:\/\/.*)&sa/)[1]
results << href.attributes["href"].value.match(/(http:\/\/.*)&sa/)[1]
end
results
rescue Exception => e
puts "Google parse failed"
e.to_s
end
end
g_results = google_results("ruby+join+array")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment