Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > finding text-segment in a HTTP::Element tree

Reply
Thread Tools

finding text-segment in a HTTP::Element tree

 
 
Arthur B.
Guest
Posts: n/a
 
      10-18-2003
Hello,
I found many exemples on how to look for specific tags using the
'look-down'. However I don't see how to write properly, using look-down,
something that would call my sub on each text-segment ( or say call my sub
and let the sub check it is dealing with a text-segment )

Tahnk you if anyone can help
 
Reply With Quote
 
 
 
 
ko
Guest
Posts: n/a
 
      10-18-2003
Arthur B. wrote:
> Hello,
> I found many exemples on how to look for specific tags using the
> 'look-down'. However I don't see how to write properly, using look-down,
> something that would call my sub on each text-segment ( or say call my sub
> and let the sub check it is dealing with a text-segment )
>
> Tahnk you if anyone can help


I could be wrong, but I don't think there is a HTTP::Element module on
CPAN (at least it doesn't show up in the first 100 doing a search).
HTML::Element has a look_down method. Is this what you are using?

What exactly are you trying to do, and what code have you tried so far?

keith

 
Reply With Quote
 
 
 
 
Arthur B.
Guest
Posts: n/a
 
      10-18-2003
ko wrote:

> I could be wrong, but I don't think there is a HTTP::Element module on
> CPAN (at least it doesn't show up in the first 100 doing a search).


HTML Element, my mistake

> HTML::Element has a look_down method. Is this what you are using?


yes

> What exactly are you trying to do,


See my post : call a sub for each text-segment, taking the segment
as a parameter. See HTML::Element for a definition of text-segment

> and what code have you tried so far?


Nothing, I can't figure how to do it

>
> keith


 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      10-19-2003
"Arthur B." <nan@voulauépa.com> wrote in message news:<bms4ac$qqr$(E-Mail Removed)>...
> ko wrote:


[snip]

> > What exactly are you trying to do,

>
> See my post : call a sub for each text-segment, taking the segment
> as a parameter. See HTML::Element for a definition of text-segment
>
> > and what code have you tried so far?

>
> Nothing, I can't figure how to do it


Two choices I'm aware of:

1. Write a recursive routine that processes all tags to deal with the
text segments.
2. Use objectify_text() to get all text segments. The method turns
text segments into HTML::Element objects and allows direct access to
the text:

#!/usr/bin/perl -w
use strict;
use HTML::TreeBuilder; # inherits from HTML::Element

my $html;
{
local $/;
$html = <DATA>;
}

my $root = HTML::TreeBuilder->new;
$root->parse($html);
$root->eof;
$root->objectify_text;
my @text_nodes = $root->look_down('_tag','~text');
# your sub to process text
print $_->attr('text'), "\n" foreach @text_nodes;

$root->deobjectify_text;
$root->delete;

__DATA__
<html>
<body>
<p>some text</p>
<p>more text</p>
</body>
</html>

Text segments are accessible through the object's 'text' attribute
using the attr() method. So to do a substitution::

(my $text = $_->attr('text')) =~ s#PATTERN#REPLACEMENT#; #read
$_->attr('text',$text); # make change

Don't know how I can explain look_down() any better than it already is
in the docs. Maybe if you copy/paste the examples from the docs into a
script and played around with it you'll get a better feel for how to
use it. Perhaps you're not too familiar with using objects? If so,
'perldoc perlboot' should help get you started.

HTH - keith
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
B+ Tree versus Ternary Search Tree Ramkumar Menon Java 2 08-16-2005 08:13 PM
B+ Tree versus Ternary Search Tree Ramkumar Menon Java 1 08-16-2005 09:46 AM
B+ Tree versus Ternary Search Tree Ramkumar Menon Java 0 08-16-2005 09:01 AM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM
Spanning Tree And Per Vlan Spanning Tree Amy L. Cisco 0 07-24-2003 10:01 PM



Advertisments