perldoc > HTML::TableParser

๐Ÿ“› NAME

HTML::TableParser - Extract data from an HTML table

๐Ÿš€ Quick Reference

Use CaseCommandDescription
Parse a file$p->parse_file('file.html')Parse HTML file and extract tables
Match table by id{ id => '1.2', ... }Select a specific table by its nested id
Match by column name{ cols => [ 'Object Type', qr/object/ ] }Match tables containing given column names
Set callbacks{ row => \&row_func, hdr => \&header_func }Define handlers for row data, headers, etc.
Enable data cleanup{ Decode => 1, Trim => 1 }Automatically decode HTML entities, trim whitespace
Use object methods{ obj => $obj, start => 'start' }Use methods on an object instead of plain functions
Create new objects per table{ class => 'MyClass', new => 'mynew' }Instantiate a new object for each matched table

๐Ÿท๏ธ VERSION

version 0.43

๐Ÿ“– SYNOPSIS

use HTML::TableParser;

@reqs = (
         {
          id    => 1.1,                    # id for embedded table
          hdr   => \&header,              # function callback
          row   => \&row,                 # function callback
          start => \&start,             # function callback
          end   => \&end,                 # function callback
          udata => { Snack => 'Food' }, # arbitrary user data
         },
         {
          id   => 1,                      # table id
          cols => [ 'Object Type',
                    qr/object/ ],       # column name matches
          obj  => $obj,                  # method callbacks
         },
        );

# create parser object
$p = HTML::TableParser->new( \@reqs,
                     { Decode => 1, Trim => 1, Chomp => 1 } );
$p->parse_file( 'foo.html' );


# function callbacks
sub start {
  my ( $id, $line, $udata ) = @_;
  #...
}

sub end {
  my ( $id, $line, $udata ) = @_;
  #...
}

sub header {
  my ( $id, $line, $cols, $udata ) = @_;
  #...
}

sub row  {
  my ( $id, $line, $cols, $udata ) = @_;
  #...
}

๐Ÿ“˜ DESCRIPTION

HTML::TableParser uses HTML::Parser to extract data from an HTML table. The data is returned via a series of user defined callback functions or methods. Specific tables may be selected either by a matching a unique table id or by matching against the column names. Multiple (even nested) tables may be parsed in a document in one pass.

๐Ÿ” Table Identification

Each table is given a unique id, relative to its parent, based upon its order and nesting. The first top level table has id 1, the second 2, etc. The first table nested in table 1 has id 1.1, the second 1.2, etc. The first table nested in table 1.1 has id 1.1.1, etc. These, as well as the tables' column names, may be used to identify which tables to parse.

๐Ÿ“ค Data Extraction

As the parser traverses a selected table, it will pass data to user provided callback functions or methods after it has digested particular structures in the table. All functions are passed the table id (as described above), the line number in the HTML source where the table was found, and a reference to any table specific user provided data.

๐Ÿ“ž Callback API

Callbacks may be functions or methods or a mixture of both. In the latter case, an object must be passed to the constructor. (More on that later.)

The callbacks are invoked as follows:

start( $tbl_id, $line_no, $udata );

end( $tbl_id, $line_no, $udata );

hdr( $tbl_id, $line_no, \@col_names, $udata );

row( $tbl_id, $line_no, \@data, $udata );

warn( $tbl_id, $line_no, $message, $udata );

new( $tbl_id, $udata );

๐Ÿงน Data Cleanup

There are several cleanup operations that may be performed automatically:

๐Ÿ—‚๏ธ Data Organization

Column names are derived from cells delimited by the <th> and </th> tags. Some tables have header cells which span one or more columns or rows to make things look nice. HTML::TableParser determines the actual number of columns used and provides column names for each column, repeating names for spanned columns and concatenating spanned rows and columns. For example, if the table header looks like this:

 +----+--------+----------+-------------+-------------------+
 |    |        | Eq J2000 |             | Velocity/Redshift |
 | No | Object |----------| Object Type |-------------------|
 |    |        | RA | Dec |             | km/s |  z  | Qual |
 +----+--------+----------+-------------+-------------------+

The columns will be:

No
Object
Eq J2000 RA
Eq J2000 Dec
Object Type
Velocity/Redshift km/s
Velocity/Redshift z
Velocity/Redshift Qual

Row data are derived from cells delimited by the <td> and </td> tags. Cells which span more than one column or row are handled correctly, i.e. the values are duplicated in the appropriate places.

๐Ÿ”ง METHODS

new

$p = HTML::TableParser->new( \@reqs, \%attr );

This is the class constructor. It is passed a list of table requests as well as attributes which specify defaults for common operations. Table requests are documented in "Table Requests".

The %attr hash provides default values for some of the table request attributes, namely the data cleanup operations ( "Chomp", "Decode", "Trim" ), and the multi match attribute "MultiMatch", i.e.,

$p = HTML::TableParser->new( \@reqs, { Chomp => 1 } );

will set Chomp on for all of the table requests, unless overridden by them. The data cleanup operations are documented above; "MultiMatch" is documented in "Table Requests".

Decode defaults to on; all of the others default to off.

parse_file

This is the same function as in HTML::Parser.

parse

This is the same function as in HTML::Parser.

๐Ÿ“‹ Table Requests

A table request is a hash used by HTML::TableParser to determine which tables are to be parsed, the callbacks to be invoked, and any data cleanup. There may be multiple requests processed by one call to the parser; each table is associated with a single request (even if several requests match the table).

A single request may match several tables, however unless the MultiMatch attribute is specified for that request, it will be used for the first matching table only.

A table request which matches a table id of "DEFAULT" will be used as a catch-all request, and will match all tables not matched by other requests. Please note that tables are compared to the requests in the order that the latter are passed to the new() method; place the DEFAULT method last for proper behavior.

๐ŸŽฏ Identifying tables to parse

HTML::TableParser needs to be told which tables to parse. This can be done by matching table ids or column names, or a combination of both. The table request hash elements dedicated to this are:

More than one of these may be used for a single table request. A request may match more than one table. By default a request is used only once (even the "DEFAULT" id match!). Set the "MultiMatch" attribute to enable multiple matches per request.

When attempting to match a table, the following steps are taken:

  1. The table id is compared to the requests which contain an id match. The first such match is used (in the order given in the passed array).
  2. If no explicit id match is found, column name matches are attempted. The first such match is used (in the order given in the passed array)
  3. If no column name match is found (or there were none requested), the first request which matches an id of "DEFAULT" is used.

๐Ÿ“ž Specifying the data callbacks

Callback functions are specified with the callback attributes "start", "end", "hdr", "row", and "warn". They should be set to code references, i.e.

%table_req = ( ..., start => \&start_func, end => \&end_func )

To use methods, specify the object with the "obj" key, and the method names via the callback attributes, which should be set to strings. If you don't specify method names they will default to (you guessed it) "start", "end", "hdr", "row", and "warn".

$obj = SomeClass->new();
# ...
%table_req_1 = ( ..., obj => $obj );
%table_req_2 = ( ..., obj => $obj, start => 'start',
                           end => 'end' );

You can also have HTML::TableParser create a new object for you for each table by specifying the "class" attribute. By default the constructor is assumed to be the class new() method; if not, specify it using the "new" attribute:

use MyClass;
%table_req = ( ..., class => 'MyClass', new => 'mynew' );

To use a function instead of a method for a particular callback, set the callback attribute to a code reference:

%table_req = ( ..., obj => $obj, end => \&end_func );

You don't have to provide all the callbacks. You should not use both "obj" and "class" in the same table request.

HTML::TableParser automatically determines if your object or class has one of the required methods. If you wish it *not* to use a particular method, set it equal to "undef". For example

%table_req = ( ..., obj => $obj, end => undef )

indicates the object's end method should not be called, even if it exists.

You can specify arbitrary data to be passed to the callback functions via the "udata" attribute:

%table_req = ( ..., udata => \%hash_of_my_special_stuff )

๐Ÿงน Specifying Data cleanup operations

Data cleanup operations may be specified uniquely for each table. The available keys are "Chomp", "Decode", "Trim". They should be set to a non-zero value if the operation is to be performed.

๐Ÿ› ๏ธ Other Attributes

The "MultiMatch" key is used when a request is capable of handling multiple tables in the document. Ordinarily, a request will process a single table only (even "DEFAULT" requests). Set it to a non-zero value to allow the request to handle more than one table.

๐Ÿ› BUGS

Please report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-TableParser> or by email to bug-HTML-TableParser AT rt.org <mailto:bug-HTML-TableParser AT rt.org>.

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

๐Ÿ“ฆ SOURCE

The development version is on github at <https://github.com/djerius/html-tableparser> and may be cloned from <git://github.com/djerius/html-tableparser.git>

๐Ÿ‘ค AUTHOR

Diab Jerius <djerius AT cpan.org>

ยฉ๏ธ COPYRIGHT AND LICENSE

This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.

This is free software, licensed under:

The GNU General Public License, Version 3, June 2007
HTML::TableParser
๐Ÿ“› NAME ๐Ÿš€ Quick Reference ๐Ÿท๏ธ VERSION ๐Ÿ“– SYNOPSIS ๐Ÿ“˜ DESCRIPTION
๐Ÿ” Table Identification ๐Ÿ“ค Data Extraction ๐Ÿ“ž Callback API ๐Ÿงน Data Cleanup ๐Ÿ—‚๏ธ Data Organization
๐Ÿ”ง METHODS
new parse_file parse
๐Ÿ“‹ Table Requests
๐ŸŽฏ Identifying tables to parse ๐Ÿ“ž Specifying the data callbacks ๐Ÿงน Specifying Data cleanup operations ๐Ÿ› ๏ธ Other Attributes
๐Ÿ› BUGS ๐Ÿ“ฆ SOURCE ๐Ÿ‘ค AUTHOR ยฉ๏ธ COPYRIGHT AND LICENSE

Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-20 19:01 @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_^