Francis Sylvester wrote:
> I'm a Perl newbie and am having a nightmare trying to get the code below
> working. I'm trying to fetch a webpage and if a link within the page matches
> the search criterion - return the text after the link.
>
> use LWP::Simple;
> use HTML::TokeParser;
Yes, using a module for parsing an HTML document is a good idea.
> my $document = get("http://www.anexamplesite.com");
> my $mymatch = "searchstring";
>
> my $parser = HTML::TokeParser->new(\$document);
>
> while ($token = $parser->get_tag("a")) {
> if ($token->[1]->{"href"} =~ /$mymatch/) {
> # print $server.$token->[1]->{href}."\n";
> $document =~ /$searchstring(.+?)someidentifier/;
What's that? After you have possibly found your search string, you let
the program search the whole document using a simple regex. Doing so
makes no sense to me.
Either you'd better stick to a simple regex, and skip the parsing
module, or (better) taking advantage of the module you are using, and
doing something like:
while ( my $token = $parser->get_tag('a') ) {
if ($token->[1]{href} =~ /$mymatch/) {
print $parser->get_text('a')."\n";
}
}
(I'm not sure if that's what you're looking for, but hopefully you get
the idea.)
--
Gunnar Hjalmarsson
Email:
http://www.gunnar.cc/cgi-bin/contact.pl