Perl programmer for hire: download my resume (PDF).
John Bokma Perl
freelance Perl programmer

Google position reporting with Perl

Results 1 - 10 of about 29,500 | 2 comments

The following Perl program reports the position(s) of a domain for a given query on Google search. The program uses the Google API via the Net-Google module.

Note that the result can be different from what you get when you use Google search via your browser since the Google API can use a different data center.

The Perl program is very barebone and improvement on the code is left as an exercise to the reader.

# gpos.pl - Reports rank using Net::Google
#
# © Copyright, 2004-2005 By John Bokma, http://johnbokma.com/
#
# $Id$ 

use strict;
use warnings;

use Net::Google;

use constant GOOGLE_LICENSE_KEY =>
    'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
use constant MAX_RESULTS => 25;


unless ( @ARGV >= 2 ) {

    print "usage: gpos.pl domain query\n";
    exit( 1 );
}

my $domain = lc shift;

my $google = Net::Google->new( key => GOOGLE_LICENSE_KEY );

my $search = $google->search();
$search->max_results( MAX_RESULTS );

my $query = $search->query( @ARGV );

print "Finding position(s) of domain '$domain' using: $query\n";

my $position = 1;

for my $result ( @{ $search->results() } ) {

    my $url = $result->URL();
    printf "%3d - %s\n", $position, $url
        if index( $url, $domain ) >= 0;
    $position++;
}

Explanation of how the Perl program works

The program consists of three parts: the first part checks the arguments given to the program, which should be two or more. The domain is removed from the program argument array (@ARGV) using shift, so the argument array only holds the query. The shift function without an argument defaults to @ARGV in this context.

The second part creates a Net::Google object initialised with the Google API key, and uses this object to create a search object. The argument array, now containing the query, is passed to the query method of the search object.

The third and final part of the Perl program retrieves the results from the search object, and prints each result if and only if the given domain is a substring of the URL returned as part of the result.

Example of usage

Example of Perl program usage and output:

gpos.pl castleamber.com ambercam
Finding position(s) of domain 'castleamber.com' using: ambercam
  1 - http://castleamber.com/ambercam.html
  2 - http://castleamber.com/

Caveat

Note that when you increase the number of results (MAX_RESULTS) that Google starts inserting extra results which might report a lower rank. The inserting of extra results seem to happen with 26 results. Note that this also happens when you use Google via your browser. I know some people prefer to set the number of results high, sometimes as high as 100. But there is quite a difference between the first 100 results when you get them 10 at a time (default) and get them all in one go, so be aware when you compare Google ranking with others.

Please post a comment | read 2 comments, latest by John Bokma | RSS feed