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
| #!/usr/bin/perl
use strict; use warnings;
use HTML::Parser;
# get argument
my $filename = shift || 'myfile.html';
# open file
open $fh, '<', $filename
or die "$!\n";
# set parser
my $parser = HTML::Parser->new( api_version => 3,
start_h => [\&start,"tagname, attr"],
);
# start handle (what should the parser do when it encouters a tag)
sub start {
my ($tag, $attr) = @_;
print $attr->{href}, "\n" if ($tag =~ /^a$/ and defined $attr->{href});
}
# parse
$parser->parse_file($fh);
# end parser
$parser->eof; |
Partager