Hi Guys,
Im trying to parse html and capture only the body excluding the body tags,,
Now below is the code ive come up with , borrowed from another post actaully, but what im trying to do in the start, text and end handlers is assign the text to a new varible with no luck, if i print my varible in each sub routine it prints but outside of that its empty .. any idea on how else I could capture this data ?
my $p = HTML::Parser->new( api_version => 3 );
$p->handler( start => \&start_handler, "self,tagname,attr" );
$p->parse($content);
exit;
my $inner_body;
sub start_handler {
my $self = shift;
my $tagname = shift;
my $attr = shift;
my $text = shift;
my $inner_body;
return unless ( $tagname eq 'body' );
$self->handler( start => sub { my ($text) = @_; $inner_bod
+y = $inner_body. $text; }, "text" );
$self->handler( text => sub { my ($text) = @_; $inner_body = $in
+ner_body. $text; }, "text" );
$self->handler( end => sub {
my ($endtagname, $self, $text) = @_;
if($endtagname eq $tagname) {
$self->eof;
} else {
$inner_body = $inner_body. $text;
}
}, "tagname,self,text");
}
print $inner_body;
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.