1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#! /usr/bin/perl
use strict;
use warnings;
use HTML::Parser;
my @links;
my $file = "my_test.html";
my $p = HTML::Parser->new();
$p->handler(start => \&start_handler,"tagname,attr,self");
$p->parse_file($file);
foreach my $link(@links){
print "Linktext: ",$link->[1],"\tURL: ",$link->[0],"\n";
}
sub start_handler{
my ($tagname,$attr,$self) = @_;
return unless($tagname eq 'a' and defined $attr);
my ($url,$class) = @{$attr}{qw/href class/};
my $text;
$self->handler(text => sub{$text = shift;},"dtext");
$self->handler(end => sub{
my ($tag) = @_;
if($tag eq 'a' and defined $class and $class eq 'res'){
push(@links,[$url,$text]);
}
},"tagname");
}