I need to do some parse some data off webpages. How do I extract text between tags using HTML::Parser?
Consider the following sample code:
#!/usr/bin/perl
use strict;
use warnings;
use HTML::Parser;
use Data::Dumper;
my $find_title = HTML::Parser->new(
api_version => 3,
start_h => [
sub {
my ($tag, $attr) = @_;
print Dumper \@_;
},
'tag'
],
);
my $html = join '',
"<html><head><title>Extract me!</title></head><body>",
(map { qq(<a href="http://$_.com">$_</a>) } qw/foo bar baz/),
"</body></html>";
$find_title->report_tags('title');
$find_title->parse($html);
How do I fix this so I can extract the title? This only extracts the tag.
You need a
text_hhandler to collect the text, and anend_hhandler to do something when the</title>tag appears (at which point the text inside the tag has been collected).HTML::Parser is a fairly low-level module, you may be happier with one of the many modules built on top of it, like HTML::TreeBuilder or HTML::TokeParser.
For example, HTML::HeadParser makes extracting the title trivial: