I have been investigating how to parse in-the-wild HTML with various Perl modules, only to become a tad dismayed. What I need to do is be able to parse a document, and retrieve the contents of specific block level elements. For example, in Javascript I would write something like:
var elem=document.getElementsByClass('className'); for (var i=0; i<elem.length; i++) { alert(elem.innerHTML); }
But I can't seem to find a sensible way of doing this in Perl. With HTML::Parser I've set up start_h and end_h event handlers, but the end_h event handler isn't smart enough to know when the specific associated end tag has appeared--so if it handles a specific <div>, I can't figure out a way to only send an event on the closing of that </div> and not any embedded ones. I am sure I'm missing something simple (wrong module likely), but my brain is frazzled enough I would appreciate some guidance. Thanks!!!

In reply to Parsing semi-complex HTML by jupe

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.