It seems you are wanting "actions" to be called based on HTML elements.

I don't know how well it works, but HTML::Parser (H::P) has options to call your "callout function" for the opening and closing tags of specific HTML elements, or all of them. When you specify the callout functions, you tell H::P what you want passed to your function. For example I wanted to see the start/stop/text and non-parsed text (DATA/javascript) so I specified functions for each (mayka is a anon-sub creation routine that included some routine error checks and such).

$p->parser(HTML::Parser->new("api_version" => 3, start_h => [ mayka($p,6,start_h => \&_start), "tag,skipped_text,attr,attrseq,line,text"], end_h => [ mayka($p,4,end_h => \&_end), "tagname,skipped_text,line,offset_end"], text_h => [ mayka($p,3, text_h => \&_text), "tag,skipped_text,text"], default_h => [ mayka($p,3,default_h => \&_dflt), "event, skipped_text, text"], marked_sections => 1,));
The last parameter specified what elements I wanted passed to my function, so for tags that had class labels, I could store & nest them.

Note -- I may easily be missing some functionality in WWW::Mechanize, but I didn't see the ability to process class or ID values when they started and ended. It does get a bit hairy trying to keep track of them, since nested elements preempt and assign class+id's to children, and when those elements end, the class+id revert to whatever was in place before you encountered that element (i.e. need to maintain a stack)...

hope this helps...


In reply to Re: html to hash table by perl-diddler
in thread html to hash table by phantom85

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.