Reputation: 15
All, I realise against gooogle ToC but I was trying to write a perl script that performs a google search and returns the number of hits (eg the 1 of about XXXX for search term). I should state I am perl newbie.
After reading etc this is what I have but it does not return any thing and I'm not sure why...can anyone give me some pointers.
use LWP::Simple;
my $ua = new LWP::UserAgent;
$ua->agent('Mozilla/5.0');
my $url=$ARGV[0];
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
$res->content;
print "all done \n";
while ($res ==~ /of about <b>([1234567890,]<\/b> +)/) {
print $res;
}
Upvotes: 0
Views: 190
Reputation: 67900
The line:
while ($res ==~ /of about <b>([1234567890,]<\/b> +)/) {
Has the following errors:
/g
global option, which would be useless
because...[1234567890,]
will only match one character, and is better written
[0-9,]+
. Note the plus sign at the end to allow multiple matches.==~
should be =~
$res
should probably be $res->content
, like a'r stated in the
comment.</b>
tag, which I suppose is
a typo?Also, you print out the whole $res
object, when you probably only want to print the number.
More correct version:
if ($res->content =~ /of about <b>([0-9,]+)/) { print $1 }
Upvotes: 3