Skip to content

Instantly share code, notes, and snippets.

@sbfaulkner
Created April 11, 2009 23:05
Show Gist options
  • Save sbfaulkner/93773 to your computer and use it in GitHub Desktop.
Save sbfaulkner/93773 to your computer and use it in GitHub Desktop.
check serp of one or more sites on google for a number of keyword queries
#!/usr/bin/env ruby
#
# check google serp for multiple sites using provided queries
#
require 'rubygems'
require 'mechanize'
SITES = [
'moneyworks.ca',
'cognito.co.nz'
]
QUERIES = [
"moneyworks",
"moneyworks canada",
"moneyworks united states",
"moneyworks us",
"accounting software",
"mac accounting software",
"accounting software canada",
"mac accounting software canada",
"accounting software united states",
"mac accounting software united states"
]
def get_position(a, query, site)
offset = 0
0.step(900,100) do |offset|
a.get("http://google.com/search?pws=0&q=#{query.gsub(/ /,'+')}&num=100&start=#{offset}") do |page|
# puts ">>>>>"
# puts page.root.css('a').collect { |l| l.inspect }.join("\n")
# puts ">>>>>"
links = page.root.css('h3.r a.l')
return nil if links.empty?
links.each_with_index do |link,i|
href = link['href']
return "[#{i + offset + 1}] #{href}" if href =~ %r"^http(?:s)?://(?:.+\.)?#{site}/.*$"
end
end
end
nil
end
a = WWW::Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' }
puts "keywords\t" + SITES.join("\t")
QUERIES.each do |q|
print "#{q}\t"
puts SITES.collect { |s| get_position(a, q, s) || '-' }.join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment