I sometimes find myself needing to extract the HTML from between the body tags of a document. The following function takes a filename, file handle, or a scalar reference (with the scalar containing the HTML).
use HTML::TokeParser::Simple 1.4; print get_contents( $some_html ); sub get_contents { my $file = shift; my $parser = HTML::TokeParser::Simple->new( $file ) or die "Cannot create parser for ($file): $!"; my $html = ''; 1 while ! $parser->get_tag->is_start_tag('body'); while ( my $token = $parser->get_token ) { last if $token->is_end_tag('body'); $html .= $token->as_is; } return $html; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extract Web page contents
by jdporter (Paladin) on Dec 28, 2002 at 05:42 UTC | |
|
Re: Extract Web page contents
by gmpassos (Priest) on Dec 27, 2002 at 11:27 UTC | |
by Aristotle (Chancellor) on Dec 27, 2002 at 22:09 UTC | |
by Ovid (Cardinal) on Dec 28, 2002 at 19:52 UTC | |
by vek (Prior) on Dec 27, 2002 at 13:21 UTC | |
by gmpassos (Priest) on Dec 27, 2002 at 13:47 UTC |