<p..><some more tags> --- TITLE --- WORDS WORDS WORDS WORDS WORDS WORD +S<BR> <object...><a lot more tags></object> </p>
WORDS section runs onto multiple lines. The code within the object tags run multiple lines as well. I'd like to grab the title, words, and object code of each instance on a page. The html is being stored in a scalar.

That's enough info for working out a basic solution with HTML::Parser. (There's still the matter of what you need to do with these pieces once you have them, but that's the easy part, right?)

It really helps to have valid html data as input for trying this out, so I made a few changes to the example you gave. Okay, this code is a lot longer than a couple regexes, but it will work, reliably, for any valid html data that resembles your example.

use strict; use HTML::Parser; my $html = <<EOH; <p foo="bar"><some more tags> --- TITLE --- WORDS WORDS WORDS WORDS WORDS WORDS<BR> <object att="val"> <and> <more> <tags/> </more> </and></object> </p> EOH my ( $partext, $objtext ); my ( @titlewords, @objects ); my $inpar = my $inobj = 0; my $parser = new HTML::Parser( api_version => 3, start_h => [ \&handle_starttag, "tagnam +e,text" ], text_h => [ \&handle_text, "dtext" ], end_h => [ \&handle_endtag, "tagname,te +xt" ] ); $parser->parse( $html ); for my $t ( @titlewords ) { print "=== Found title and words: ===\n$t\n======\n"; } for my $o ( @objects ) { print "=== Found object: ===\n$o\n======\n"; } sub handle_starttag { my ( $tag, $text ) = @_; if ( $tag eq 'p' ) { $inpar = 1; $partext = ''; } elsif ( $tag eq 'object' ) { $inobj = 1; $objtext = ''; } elsif ( $tag eq 'br' and $inpar ) { push @titlewords, $partext if ( $partext =~ /-+ TITLE -+/ ); $inpar = 0; } elsif ( $inobj ) { $objtext .= $text; } } sub handle_text { my ( $text ) = @_; if ( $inpar ) { $partext .= $text; } elsif ( $inobj ) { $objtext .= $text; } } sub handle_endtag { my ( $tag, $text ) = @_; if ( $tag eq 'object' ) { push @objects, $objtext; $inobj = 0; } elsif ( $inobj ) { $objtext .= $text; } }

Look at the man page for HTML::Parser to understand how the "new" call works. The rest is just a matter of figuring out how to handle the data contents, as the parser encounters the relevant tag and text events.

(Updated the code to check for "TITLE" in the text when there's a "br" tag, and added line-breaks in the html to show how those would be handled.)


In reply to Re: Trying to parse html file by graff
in thread Trying to parse html file by annunaki10

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.