我正在尝试解析 HTML 文档并提取 URL,但无法弄清楚如何获取“a”标签的 href 值。如果有帮助,我正在查看此文档:http: //perl.active-venture.com/lib/HTML/TokeParser.html
问题出在第三个“elsif”代码块中。
sub findTokens {
my $htmlFileName = $_[0];
my $pagesDir = $_[1];
my %titles = %{$_[2]};
my %mapping = %{$_[3]};
my @outLinks;
my $p = HTML::TokeParser->new("$pagesDir$htmlFileName")
or die("Can't open $htmlFileName: \n");
my @tokens;
my $url = $mapping{$htmlFileName};
while (my $newChunk = $p->get_token) {
if ($newChunk->[0] eq 'T') {
my @lineArray = split(' ', $newChunk->[1]);
foreach my $i (@lineArray) {
if ( lc($i) =~ /^[a-z]*\-?\'?s?$/) {
push(@tokens, lc($i));
}
}
} elsif ($newChunk->[0] eq 'S') {
if ($newChunk->[1] eq 'title') {
$newChunk = $p->get_token;
$titles{$url} = $newChunk->[1];
#print $url;
#print $titles{$url};
}
} elsif ($newChunk->[0] eq 'S') {
if ($newChunk->[1] eq 'a') {
my %attr = %{$newChunk->[2]};
push(@outLinks, $attr{'href'});
}
}
}
return (\@tokens, \%titles, \@outLinks);
}