Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Pulling part of another web page into your output on the fly (with HTML::Parser)

by grinder (Bishop)
on Sep 20, 2005 at 15:16 UTC ( #493480=CUFP: print w/ replies, xml ) Need Help??

Well this is pretty nifty.

I am building a workflow application at work, and the heart of the matter is dealt with by a Perl script on the web. The documentation sits in a webified Lotus database on another machine. I have to include the documentation from the Lotus front page in my script.

It was obvious that HTML::Parser was the answer, but it took a bit of fiddling about to get it right. The thing to remember is that a decent module should always include an eg/ directory to help you out. By mulling over the contents of the examples included with H::P I was able to come up with the following code.

The idea is to look at the tags as they go by, and keep an eye out when we enter <script> or <body> tags. While I was writing this, I found it helpful to use the DEBUG setting, to change the opening less-than character of a tag to an left square bracket, which made it easier to see what was happening.

I'm not a heavy user of H::P; this is perhaps the most complex thing I've ever done with it, so I'm quite pleased the way it turned out. See if you can spot the pun in the code.

use strict; use LWP::Simple; use HTML::Parser; use constant DEBUG => 0; my $URI_HOST = 'http://www.example.com'; my $URI_PATH = '/sample/database.nsf'; my $content = get( "$URI_HOST$URI_PATH" ); if( not $content ) { $content = q{<p><small>The documentation is unavailable</small></p +>}; } else { my $new = ''; my %in_tag; my $tag_watch = sub { my $tag = shift; my $in = shift; my $attr = shift; $in_tag{$tag} += $in; return unless $in_tag{script} or $in_tag{body} or grep { $tag eq $_ } qw( script body ) ; my $out = (DEBUG ? '[' : '<') . ($in < 1 ? '/' : '') . $tag; if( $attr ) { my @attr_pair; while( my( $key, $value ) = each %$attr ) { # renormalise things that look like URIs $value =~ s{^($URI_PATH/)}{$URI_HOST$1}; push @attr_pair, qq{$key="$value"}; } $out .= join( ' ', ('', @attr_pair)); } $out .= '>'; $new .= $out; }; my $p = HTML::Parser->new( api_version => 3, start_h => [$tag_watch, "tagname, '+1', attr"], end_h => [$tag_watch, "tagname, '-1'"], text_h => [sub { $new .= $_[0] if $in_tag{script} or $in_ta +g{body} }, "dtext"], comment_h => [sub { $new .= $_[0] if $in_tag{script} }, + "dtext"], ); $p->parse($content); $p->eof(); $content = $new; } # now include $content in a templating/include output mechanism of you +r choice

• another intruder with the mooring in the heart of the Perl

Comment on Pulling part of another web page into your output on the fly (with HTML::Parser)
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://493480]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-23 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls