Skip to content

Instantly share code, notes, and snippets.

@Jamedjo
Created July 26, 2013 16:04
Show Gist options
  • Save Jamedjo/6090070 to your computer and use it in GitHub Desktop.
Save Jamedjo/6090070 to your computer and use it in GitHub Desktop.
Scrape top google results for title, meta-description and keywords. Usage: `ruby scrape.rb your search terms go here`
#!/usr/bin/ruby
require 'nokogiri'
require 'open-uri'
#require 'debugger'
require 'cgi'
url = "http://www.google.co.uk/search?q=#{ARGV.join('+')}"
puts url
doc = Nokogiri::HTML(open(url))
# debugger
doc.css('.g .r a').each{|l| puts l.content}
puts "Scanning..."
doc.css('.g .r a').each do |item|
link= CGI.parse(URI.parse(item['href']).query)['q'].first
doc = Nokogiri::HTML(open(link))
puts "title: " + doc.title.to_s
puts "meta-title: " + doc.xpath('//meta[@name="title"]/@content').map(&:value).to_s
puts "keywords: " + doc.xpath('//meta[@name="keywords"]/@content').map(&:value).to_s
puts "description: " + doc.xpath('//meta[@name="description"]/@content').map(&:value).to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment