perldoc > HTML::Parser

๐Ÿ“› NAME

HTML::Parser - HTML parser class

๐Ÿš€ Quick Reference

Use CaseCommandDescription
Strip all comments$p->handler(comment => "");Ignore comment markup entirely
Extract <title> text$p->handler(start => \&start_handler, "tagname,self");Set up handler to capture title content
Parse a file$p->parse_file("index.html");Read and parse entire HTML file
Stream parsing$p->parse($chunk);Feed document chunk by chunk
Ignore script/style tags$p->ignore_elements(qw(script style));Suppress all events for these elements
Decode text entities$p->handler(text => sub { ... }, "dtext");Get text with entities decoded
Report only certain tags$p->report_tags(qw(a img));Only receive events for listed tags
Enable XML mode$p->xml_mode(1);Treat tags as caseโ€‘sensitive, empty elements etc.

๐Ÿ“œ SYNOPSIS

use strict;
use warnings;
use HTML::Parser ();

# Create parser object
my $p = HTML::Parser->new(
  api_version => 3,
  start_h => [\&start, "tagname, attr"],
  end_h   => [\&end,   "tagname"],
  marked_sections => 1,
);

# Parse document text chunk by chunk
$p->parse($chunk1);
$p->parse($chunk2);
# ...
# signal end of document
$p->eof;

# Parse directly from file
$p->parse_file("foo.html");
# or
open(my $fh, "<:utf8", "foo.html") || die;
$p->parse_file($fh);

๐Ÿ“– DESCRIPTION

Objects of the HTML::Parser class will recognize markup and separate it from plain text (alias data content) in HTML documents. As different kinds of markup and text are recognized, the corresponding event handlers are invoked.

HTML::Parser is not a generic SGML parser. We have tried to make it able to deal with the HTML that is actually "out there", and it normally parses as closely as possible to the way the popular web browsers do it instead of strictly following one of the many HTML specifications from W3C. Where there is disagreement, there is often an option that you can enable to get the official behaviour.

The document to be parsed may be supplied in arbitrary chunks. This makes onโ€‘theโ€‘fly parsing as documents are received from the network possible.

If event driven parsing does not feel right for your application, you might want to use HTML::PullParser. This is an HTML::Parser subclass that allows a more conventional program structure.

๐Ÿ› ๏ธ METHODS

The following method is used to construct a new HTML::Parser object:

If a top level key is in the form <event>_h (e.g., text_h) then it assigns a handler to that event, otherwise it initializes a parser option. The event handler specification value must be an array reference. Multiple handlers may also be assigned with the handlers => [%handlers] option. See examples below.

If new() is called without any arguments, it will create a parser that uses callback methods compatible with version 2 of HTML::Parser. See the section on version 2 compatibility below for details.

The special constructor option api_version => 2 can be used to initialize version 2 callbacks while still setting other options and handlers. The api_version => 3 option can be used if you donโ€™t want to set any options and donโ€™t want to fall back to v2 compatible mode.

Examples:

$p = HTML::Parser->new(
  api_version => 3,
  text_h => [ sub {...}, "dtext" ]
);

This creates a new parser object with a text event handler subroutine that receives the original text with general entities decoded.

$p = HTML::Parser->new(
  api_version => 3,
  start_h => [ 'my_start', "self,tokens" ]
);

This creates a new parser object with a start event handler method that receives the $p and the tokens array.

$p = HTML::Parser->new(
  api_version => 3,
  handlers => {
    text => [\@array, "event,text"],
    comment => [\@array, "event,text"],
  }
);

This creates a new parser object that stores the event type and the original text in @array for text and comment events.

The following methods feed the HTML document to the HTML::Parser object:

Most parser options are controlled by boolean attributes. Each boolean attribute is enabled by calling the corresponding method with a TRUE argument and disabled with a FALSE argument. The attribute value is left unchanged if no argument is given. The return value from each method is the old attribute value.

Methods that can be used to get and/or set parser options are:

As markup and text is recognized, handlers are invoked. The following method is used to set up handlers for different events:

Examples:

$p->handler(start =>  "start", 'self, attr, attrseq, text' );

This causes the start method of object $p to be called for start events. The callback signature is $p->start(\%attr, \@attr_seq, $text).

$p->handler(start =>  \&start, 'attr, attrseq, text' );

This causes subroutine start() to be called for start events. The callback signature is start(\%attr, \@attr_seq, $text).

$p->handler(start =>  \@accum, '"S", attr, attrseq, text' );

This causes start event information to be saved in @accum. The array elements will be ['S', \%attr, \@attr_seq, $text].

$p->handler(start => "");

This causes start events to be ignored. It also suppresses invocations of any default handler for start events. It is in most cases equivalent to $p->handler(start => sub {}), but is more efficient. It is different from the emptyโ€‘subโ€‘handler in that skipped_text is not reset by it.

$p->handler(start => undef);

This causes no handler to be associated with start events. If there is a default handler it will be invoked.

Filters based on tags can be set up to limit the number of events reported. The main bottleneck during parsing is often the huge number of callbacks made from the parser. Applying filters can improve performance significantly.

The following methods control filters:

Internally, the system has two filter lists, one for report_tags and one for ignore_tags, and both filters are applied. This effectively gives ignore_tags precedence over report_tags.

Examples:

$p->ignore_tags(qw(style));
$p->report_tags(qw(script style));

results in only script events being reported.

๐Ÿงพ Argspec

Argspec is a string containing a commaโ€‘separated list that describes the information reported by the event. The following argspec identifier names can be used:

The whole argspec string can be wrapped up in @{...} to signal that the resulting event array should be flattened. This only makes a difference if an array reference is used as the handler target. Consider this example:

$p->handler(text => [], 'text');
$p->handler(text => [], '@{text}');

With two text events; "foo", "bar"; then the first example will end up with [["foo"], ["bar"]] and the second with ["foo", "bar"] in the handler target array.

๐Ÿ“ก Events

Handlers for the following events can be registered:

๐ŸŒ Unicode

HTML::Parser can parse Unicode strings when running under perlโ€‘5.8 or better. If Unicode is passed to $p->parse() then chunks of Unicode will be reported to the handlers. The offset and length argspecs will also report their position in terms of characters.

It is safe to parse raw undecoded UTFโ€‘8 if you either avoid decoding entities and make sure to not use argspecs that do, or enable the utf8_mode for the parser. Parsing of undecoded UTFโ€‘8 might be useful when parsing from a file where you need the reported offsets and lengths to match the byte offsets in the file.

If a filename is passed to $p->parse_file() then the file will be read in binary mode. This will be fine if the file contains only ASCII or Latinโ€‘1 characters. If the file contains UTFโ€‘8 encoded text then care must be taken when decoding entities as described in the previous paragraph, but better is to open the file with the UTFโ€‘8 layer so that it is decoded properly:

open(my $fh, "<:utf8", "index.html") || die "...: $!";
$p->parse_file($fh);

If the file contains text encoded in a charset besides ASCII, Latinโ€‘1 or UTFโ€‘8 then decoding will always be needed.

๐Ÿ”„ VERSION 2 COMPATIBILITY

When an HTML::Parser object is constructed with no arguments, a set of handlers is automatically provided that is compatible with the old HTML::Parser version 2 callback methods.

This is equivalent to the following method calls:

$p->handler(start   => "start",   "self, tagname, attr, attrseq, text");
$p->handler(end     => "end",     "self, tagname, text");
$p->handler(text    => "text",    "self, text, is_cdata");
$p->handler(process => "process", "self, token0, text");
$p->handler(
  comment => sub {
    my($self, $tokens) = @_;
    for (@$tokens) {$self->comment($_);}
  },
  "self, tokens"
);
$p->handler(
  declaration => sub {
    my $self = shift;
    $self->declaration(substr($_[0], 2, -1));
  },
  "self, text"
);

Setting up these handlers can also be requested with the api_version => 2 constructor option.

๐Ÿงฌ SUBCLASSING

The HTML::Parser class is able to be subclassed. Parser objects are plain hashes and HTML::Parser reserves only hash keys that start with _hparser. The parser state can be set up by invoking the init() method, which takes the same arguments as new().

๐Ÿ’ก EXAMPLES

The first simple example shows how you might strip out comments from an HTML document. We achieve this by setting up a comment handler that does nothing and a default handler that will print out anything else:

use HTML::Parser;
HTML::Parser->new(
  default_h => [sub { print shift }, 'text'],
  comment_h => [""],
)->parse_file(shift || die) || die $!;

An alternative implementation is:

use HTML::Parser;
HTML::Parser->new(
  end_document_h => [sub { print shift }, 'skipped_text'],
  comment_h      => [""],
)->parse_file(shift || die) || die $!;

This will in most cases be much more efficient since only a single callback will be made.

The next example prints out the text that is inside the <title> element of an HTML document. Here we start by setting up a start handler. When it sees the title start tag it enables a text handler that prints any text found and an end handler that will terminate parsing as soon as the title end tag is seen:

use HTML::Parser ();

sub start_handler {
  return if shift ne "title";
  my $self = shift;
  $self->handler(text => sub { print shift }, "dtext");
  $self->handler(
    end  => sub {
      shift->eof if shift eq "title";
    },
    "tagname,self"
  );
}

my $p = HTML::Parser->new(api_version => 3);
$p->handler(start => \&start_handler, "tagname,self");
$p->parse_file(shift || die) || die $!;
print "\n";

On a Debian box, more examples can be found in the /usr/share/doc/libhtml-parser-perl/examples directory. The program hrefsub shows how you can edit all links found in a document and htextsub how to edit the text only; the program hstrip shows how you can strip out certain tags/elements and/or attributes; and the program htext show how to obtain the plain text, but not any script/style content.

You can browse the eg/ directory online from the [Browse] link on the http://search.cpan.org/~gaas/HTML-Parser/ page.

๐Ÿ› BUGS

๐Ÿฉบ DIAGNOSTICS

The following messages may be produced by HTML::Parser. The notation in this listing is the same as used in perldiag:

๐Ÿ”— SEE ALSO

HTML::Entities, HTML::PullParser, HTML::TokeParser, HTML::HeadParser, HTML::LinkExtor, HTML::Form

HTML::TreeBuilder (part of the HTMLโ€‘Tree distribution)

<http://www.w3.org/TR/html4/>

More information about marked sections and processing instructions may be found at <http://www.is-thought.co.uk/book/sgml-8.htm>.

ยฉ๏ธ COPYRIGHT

Copyright 1996โ€‘2016 Gisle Aas. All rights reserved.
Copyright 1999โ€‘2000 Michael A. Chase. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

HTML::Parser
๐Ÿ“› NAME ๐Ÿš€ Quick Reference ๐Ÿ“œ SYNOPSIS ๐Ÿ“– DESCRIPTION ๐Ÿ› ๏ธ METHODS
๐Ÿงพ Argspec ๐Ÿ“ก Events ๐ŸŒ Unicode
๐Ÿ”„ VERSION 2 COMPATIBILITY ๐Ÿงฌ SUBCLASSING ๐Ÿ’ก EXAMPLES ๐Ÿ› BUGS ๐Ÿฉบ DIAGNOSTICS ๐Ÿ”— SEE ALSO ยฉ๏ธ COPYRIGHT

Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-20 18:57 @2600:1f28:365:80b0:8802:8bb4:3873:328e
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^