Today is: 16 January, 2012
Check todays hot topics

Revised: A quick perl script for used car research on craigslist

Today I stumbled across Nick Bernstein's website. He seems like a cool guy and I can't wait to play with his netapp tool.

I was taking a peak at his blog and saw he had a perl script for finding car price averages on craigslist. I thought this was a pretty neat idea considering I find myself in the same position. While we appreciate his ghettocode :) I thought I could make it moar bettah!

This was driven by two things:
1) I like perl scripts that scrape websites and do things.
2) I hate making syscalls in perl(``)!

I didn't touch his regex in the middle portions because I didn't feel like staring at HTML source to fix up this script. I thought it was strange he was splitting on space. I toyed with the idea of using HTML::TokeParser for this, but decided I was lazy and haven't yet had my Monster this morning.

You can find his original code at the link above.

Here is my take on this:

#!/opt/local/bin/perl
 
use strict;
use warnings;
 
use Data::Dumper;
use LWP::UserAgent;
 
my $default = "Hunk of shit car";
$\ = "\n";
 
sub main {
        my $lowest = 0;
        my $highest = 0;
        my $amt = 0;
        my $count = 0;
        my $average;
        my $url = "http://losangeles.craigslist.org/search/cta?query=";
        my $search_term = $ARGV[0] ? $ARGV[0] : $default;
        $search_term =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
        my $query = join('', $url, $search_term);
 
        my $ua = LWP::UserAgent->new( agent => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' );
        my $resp = $ua->get($query);
        my $shizzle = $resp->content() if ($resp->is_success());
        die("Found no listings matching $search_term\n") if ($shizzle =~ /Nothing found for that search/i);
        foreach my $word (split(/\s/, $shizzle)) {
                if ( $word =~ m/^\$/) {
                        $word =~ s/(\$|,)//g ;
                        if ( $word =~ m/^\d+$/ ) {
                                if ($lowest eq 0) {
                                        $lowest = $word ;
                                } elsif ( $word < $lowest ) {
                                        $lowest = $word ;
                                }
                                if ($highest eq 0) {
                                        $highest = $word ;
                                } elsif ( $word > $highest ) {
                                        $highest = $word ;
                                }
                                $amt += $word ;
                                $count++;
                        }
                }
        }
        $average = ( $amt / $count ) ;
        print "Found a total of $count matches.";
        print "lowest:\t\t\$$lowest";
        print "highest:\t\$$highest";
        printf("Average:\t\$%.2f", $average);
        print "\n\n";
}
 
main();

I was thinking I could include more results then just the first page it finds with minimal effort. If I get around to it I'll post it up.

AttachmentSize
craigslist.pl.txt1.3 KB