Created
July 26, 2013 16:04
-
-
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`
This file contains hidden or 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
#!/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