Get alexa rank from alexa.com with perl.

This is just a little code snippet to return the alexa rank of a given site from http://alexa.com. The script should be easily adaptable to whatever you’re using it for, but let me know if you have any questions.

#!/usr/bin/perl
use strict;
use LWP::Simple;
use URI::URL;
my $need_alexa_rank = $ARGV[0];
my $url = url("http://alexa.com/siteinfo/$need_alexa_rank");
my $page = get($url) or die ("Couldn't get it!\n");
my @lines = split('\n', $page);
foreach (@lines) {
 if (/\<div class\=\"data .*?\"\>/) {
   if (/([0-9,]+)/) {
     print $1, "\n";
   } else {
     print "idk\n";
   }
 }
}

If you have a quicker way to do this, I would be happy to see the solution. The two areas that can be improved are: 1) fetching the page takes forever because I use LWP, and 2) I don’t think splitting the entire page on newline and searching for a tag is very quick. If you can make it better please comment!

This entry was posted in Programming, Webhosting and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.