Google ranking tool - Source code

| No Comments

If you are a SEO professional one of the many parts of your job is actually measuring your success. Ok so your client is ranked on the first page of 'Maryland Personal Injury Lawyer' now, but what were they ranked when you started? What I do whenever I begin working with a new client is to take all their keyword phrases and see what their current position is. Then on a weekly basis I will run that report again and see our progress. How do we do this? Well we don't manually search Google fo reach keyword instead we use a great little program to do the work for us. Lets take a look

The major problem with finding out our Google ranking on our keywords is that Google really doesn't give us an easy way to do this anymore. Once apon a time Google had a great SOAP API that would allow us to query Google directly from Perl and return the results easily. In fact that is exactly what my program does. Unfortunately Google has stopped issuing SOAP API keys. Why? Well instead they have an AJAX API but it doesn't come close to offering the same functionality. The only other method would be to scrape Google which is a bad thing. Scraping is when you write a program to literally act like a web browser and interact with a web site, get your data and do what you need to do with it. Sounds great. Well it is - it really is. However this is against Google's terms, and I'm just not up for going against Google. So for the purpose of this program we'll assume you have a Google SOAP API key.

The goal of giving out this source code is really two fold. First of all if you have a SOAP API key this is a great small program to help you track your rankings. Secondly its a great opportunity to learn Perl. I'll do more posts on Perl and programming and all that good stuff later, for now lets take a look at the program itself.

This program assumes:

The Source Code

use LWP::Simple;  
use HTML::Parser (); 
use Net::Google; 
$domainname = $ARGV[0] ; 
$filenameis = $ARGV[1]; 
use constant GOOGLE_LICENSE_KEY => 'YOURAPIKEY'; 
use constant MAX_RESULTS => 50;
Ok above this comment we have some basic setup lines here. As you can see it loads our modules and retrieves two command line arguments. I made this so when you run the program the first argument is the domain name you are looking for and the second is a test file full of keywords (separated by a new line). You can also see where I have the x's - that is where you should paste your Google SOAP API key.

my $google = Net::Google->new( key => GOOGLE_LICENSE_KEY );
$domain = $domainname;
open(FILE, $filenameis) || die("Could not open file!");
@content=<FILE>;
Ok so we open the file and go through every single line. This will take each keyword phrase and run it against Google. You can see in the first section we are pulling a maximum amount of 50 results which is 5 pages.
foreach $line(@content) {
my $search = $google->search();
$search->max_results( MAX_RESULTS );

Runs the query against the keyword phrase (variable $line);

my $query = $search->query( $line);
my $position = 1;

This printf $line will print out the keyword phrase we are searching for.

printf $line;

The results are now cycled through a 'for' loop and stored into the $url variable.

for my $result ( @{ $search->results() } ) {
my $url = $result->
URL();

This next line searches through the URL of the result and looks for our domain, if found it prints out the position. If not it adds one to the position counter and checks the next URL result. It'll do this up to 50 results then go to the next keyword.

if($url =~ /$domain/) {
printf "%3d \n", $position;
}
$position++;
}
}

 So as you can see its a pretty simple script. Hopefully I explained that well enough. If you have any questions please shoot me an email or leave a comment. Thanks!

Download source code for this script

google-search.pl

 How to use this script

The tutorial goes over how it works, but if you don't really want to know how to it works you just want to use it - I'll get you started. Again we assume you have Perl and the required module installed, if not I'll do a seperate post about that. Make a text file and name it whatever you want, for example keywords.txt. Fill it with keyword phrases, put one on each line. So the first line would say 'Maryland Injury Lawyer' (without the quotes), second line could be 'Baltimore Injury Lawyer', etc. You get the idea. Now just start the script and specify your domain name then the keyword text file you just named so here we go.

Linux

google-search.pl millerandzois.com mzkeywords.txt

This will run the program and search for millerandzois.com and use all the keywords found in mzkeywords.txt.

Windows

If you use Windows its the same but you have to start Perl first so for example

perl google-search.pl millerandzois.com mzkeywords.txt

That's it for now I know I'm assuming already you have a lot of things already setup if you don't have those things I promise I will make a post explaining how to setup and use Perl. For those of you not using Perl I highly recommend it. Its very easy to use as shown and can really work well in a bunch of different scenarios.

About this Entry

This page contains a single entry by Tom Fitzgerald published on June 27, 2009 6:42 AM.

Getting a conversion number from a percentage was the previous entry in this blog.

Bing + Live VS Google is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.