#!/usr/bin/perl use warnings; use strict; # goolies: check popularity of words on Google. modified from: # Example code from Chapter 2 of /Perl and LWP/ by Sean M. Burke # http://www.oreilly.com/catalog/perllwp/ # sburke@cpan.org use LWP; use URI::Escape; die "Usage: goolies word1 word2\n" unless @ARGV; foreach my $word (@ARGV) { next unless length $word; my $url = 'http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=' . uri_escape($word) . '&btnG=Google+Search'; my ($content, $status, $is_success) = do_GET($url); if (!$is_success) { print "Sorry, failed: $status\n"; } elsif ($content =~ m#of about (\d+,?\d*,?\d*)#) { print "$word: $1 matches\n"; } else { print "$word: couldn't extract count due to some weird shit at $url\n"; } sleep 2; # be nice to Google's servers!!! } ##### subs ##### sub do_GET { my $browser = LWP::UserAgent->new(); $browser->agent('Mozilla/4.76 [en] (Win98; U)'); # tricked you! my $response = $browser->get(@_); return ($response->content, $response->status_line, $response->is_success, $response) if wantarray; return unless $response->is_success; return $response->content; }