Created
June 10, 2012 18:53
-
-
Save ryanjones/2906912 to your computer and use it in GitHub Desktop.
Parse out the hrefs from a google search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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