All pastes #132935 Raw Edit

googler.pl

public text v1 · immutable
#132935 ·published 2006-08-15 15:27 UTC
rendered paste body
#!/usr/bin/perl
# googler.pl - google script

use warnings;
use strict;
use LWP 5.64;

my $term;

if(!@ARGV) {
print "Search term: ";
$term = <STDIN>;
chomp($term);
$term =~ s/\s/+/i;
}
else {
$term = join("+", @ARGV);
}

my $url = "http://www.google.co.uk/search";
my $term_url = "http://www.google.co.uk/search?q=$term&ie=UTF-8&oe=UTF-8";
# doing it with POST data variables would be better

my $browser = LWP::UserAgent->new;
$browser->agent('Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.4 (like Gecko)');
# pretend to be konqueror, so google will let use have the page

my $response = $browser->get($term_url);

die "$url error: ", $response->status_line unless $response->is_success;
# die if we didn't get the response

my @split_one = split('<p class=g><!--m--><a class=l href="', $response->content);
# this always appears before the link
shift(@split_one);
# the first split part is the start of the html doc, get rid of it

my @links;

foreach my $split (@split_one) {
my @split_two = split('">', $split);
my $link = $split_two[0];
push(@links, $link);
}

print join("\n", @links);
print "\n";
exit;