in reply to Perl code to check and count count if a word exists on a webpage

If you want to see if a word is on a webpage, the first step is to get the webpage. There are many Perl tools for doing that. The most basic of these is: LWP::Simple. There are other tools, Mechanize is one, also LWP::UserAgent.

This whole subject can get complicated if you have to fill in forms and do more complex types of interactions with the web server. You didn't provide any information about what kind of webpage's you want to access. But basically it is possible to get any page that you as a human could while clicking around on a website. LWP Cookbook may be useful. Let us know how you get on after trying some simple stuff.

Update:
As possible alternate to your code, see below. No need to split, you can use a match global. The \Q\E pair says to interpret text in $word literally (in case funny characters are in there). The \b says to match on a word boundary - this is why the "thisthis" does not match.

#!/usr/bin/perl -w use strict; my $count; my $word = "this"; while (<DATA>) { my @matches = /\b\Q$word\E\b/gi; $count += @matches; # # or just do this: # $count+= () = /\b\Q$word\E\b/gi; } print "$count"; #prints 3 # note that the last "thisthis" on line 1 # doesn't count! Also note that multiple "this" on # same line are counted. __DATA__ this is the text that this thisthis abd this
This is of course just the functionality of command line grep. There are Windows variants free on the web - search Windows grep, or gnu grep.
  • Comment on Re: Perl code to check and count count if a word exists on a webpage
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl code to check and count count if a word exists on a webpage
by planetscape (Chancellor) on Jan 29, 2012 at 12:50 UTC