I would take a serious look at HTML::Tree (which is composed of HTML::TreeBuilder and HTML::Element). It runs on top of HTML::Parser and has lots of great functions that can do exactly what your are looking for, as well as lots of other tree-walking and manipulating magic.

For example, you could do something like this (untested):

use HTML::Tree; my $tree = HTML::Tree->new_from_file( "myfile.html"); my $html_object = $tree->look_down("_tag", "x", # find an x element + "y", qr/z/); # that has a y attribut +e matching z
IIRC, you can do the same thing in list contest and get a list of all the <y></y> elements in the file, or in a particular leaf of the tree.

Then you can pull whatever you want out (ie. other attributes) of your $html_object, see it as text:

my $text = $html_object->as_text;
or as raw HTML:
my $html = $html_object->as_HTML;

There is a bit of a learning curve (or at least there was for me), in that you have to get used to thinking of your document as a tree, and not as a text per se. But once you've get it, you can do a lot of things very cleanly.


In reply to Re: HTML::Parser API script or module by skillet-thief
in thread HTML::Parser API script or module by ldln

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.