user1064457
user1064457

Reputation: 15

Return string on web page between two different words/strings in perl

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

Answers (1)

TLP
TLP

Reputation: 67900

The line:

while ($res ==~ /of about <b>([1234567890,]<\/b> +)/) {

Has the following errors:

  • While statement will cause an endless loop if the string matches, because...
  • ...the regex lacks the /g global option, which would be useless because...
  • ...I assume this match will happen only once.
  • [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.
  • Your capture group extends beyond the </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

Related Questions