I think that for many HTML tasks HTML::TokeParser is a good module, it is a slightly simpler interface to HTML::Parser. When you parse with HTML::Parser you have to have all of your handlers and logic set up before you can call parse(). But HTML::TokeParser will turn the entire HTML document into a stack of tokens that you can shift and unshift as needed. The tokens correspond to each element of your HTML page.

Because your program's logic is based on just a few criteria happening in an order, this is the perfect time for that. Here is some pseudocode that would use HTML::TokeParser:

#Get HTML Document #HTML::TokeParser->new( HTML ) #get first Token #until ( TokenTag eq 'h3' and TokenText eq 'foo') # get next Token #if EndOfHTML then exit #until ( TokenTag eq 'em' ) # get next Token #declare EM container #until ( get TokenTag eq 'hr' ) # add TokenText to EM container # get next Token #use EM container

As you can see, this logic is a lot simpler than trying to set up handlers for each of the tags you care about, then trying to manage states-- which is about what you have to do with HTML::Parser.


In reply to (ichimunki) Re: Parse... then what? (HTML Parsing problems) by ichimunki
in thread Parse... then what? (HTML Parsing problems) by Chady

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.