简体   繁体   中英

How can I extract text between tags using HTML::Parser?

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_h handler to collect the text, and an end_h handler 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:

use strict;
use warnings;

use HTML::HeadParser;

my $html = join '',
    "<html><head><title>Extract me!</title></head><body>",
    (map { qq(<a href="http://$_.com">$_</a>) } qw/foo bar baz/),
    "</body></html>";

my $p = HTML::HeadParser->new;
$p->parse($html);

my $title = $p->header('Title');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM