James Bachini

Ruby & Selenium Script to Check Google SERP ranks

Haven’t used this in a while but it used to work really well untill google would throw a captcha at you after the first 5000 queries. Might be of use to someone. It’s designed for linux and you’ll need selenium up and running to work it.

#!/usr/bin/env ruby
require ‘rubygems’
gem ‘selenium-client’
require ‘selenium/client’

system(‘java -jar ‘ + File.dirname(__FILE__) + ‘/../../selenium-server.jar &’)
system(‘sleep 3’)

$quickcheckres = Array.new
#logfile
dcom = ‘date +%D’
dres = IO.popen(dcom)
date = dres.readlines.to_s.gsub(‘/’, ‘_’)
date.chomp!
logfile = File.dirname(__FILE__) + ‘/logs/quick’ + date + ‘.txt’

class Array
def shuffle!
size.downto(1) { |n| push delete_at(rand(n)) }
self
end
end

file2 = File.open(File.dirname(__FILE__) + ‘/mydomains.txt’)
$mydomains = file2.readlines
file2.close
#puts ‘Checking domains…’
#puts $mydomains

file3 = File.open(File.dirname(__FILE__) + ‘/mysearches.txt’)
$mysearches = file3.readlines
file3.close
#puts ‘Checking searches’
#puts $mysearches

@selenium = Selenium::Client::Driver.new(“localhost”, 4444, “*chrome”, “http://www.google.co.uk/”, 10000);
@selenium.start

$mysearches.each { |search|
search.chomp! # experimental 26.2.10
gsearch = “/search?num=100&h1=en&q=” + search.gsub(‘ ‘, ‘+’)
@selenium.open gsearch
@selenium.wait_for_page_to_load “30000”
# system ‘sleep 10’

html = @selenium.get_body_text()
html2 = html.to_s
#puts html2
test = 0
#puts html
$mydomains.each { |dom|
#puts dom
dom.chomp!
if html2.include? dom
test = 1
$mydom = dom
end
}
if test == 0
puts ‘No results for ‘ + search
logcom = ‘echo “No results for ‘ + search + ‘” >> ‘ + logfile
system(logcom)
$quickcheckres << ‘No results for ‘ + search
else
#   puts ‘Result found for ‘ + search
crf = ‘Cached – Similar’ #change common result factor
html3 = html2.split crf
rank = 1
html3.each { |cr|
if cr.include? $mydom
puts $mydom + ‘ ranks no.’ + rank.to_s + ‘ for ‘ + search
$quickcheckres << $mydom + ‘ ranks no.’ + rank.to_s + ‘ for ‘ + search
logcom = ‘echo “‘ + $mydom + ‘ ranks no.’ + rank.to_s + ‘ for ‘ + search + ‘” >> ‘ + logfile
system(logcom)

else
rank = rank + 1
end
}
end

}

#close server
sys = ‘ps’
sysres = IO.popen(sys)
sysres2 = sysres.readlines
sysres2.each { |sr|
if sr.include? ‘java’
sr2 = sr.split
system (‘kill ‘ + sr2[0])
end
}

puts $quickcheckres


Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.


Posted

in

by