Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Truncating an HTML node using XPaths in HTML::TreeBuilder::XPath

by mldvx4 (Friar)
on Sep 27, 2019 at 18:33 UTC ( [id://11106798]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Truncating an HTML node using XPaths in HTML::TreeBuilder::XPath
in thread Truncating an HTML node using XPaths in HTML::TreeBuilder::XPath

Neat. That's close but takes the first part of the node, and only the first part of the node, not necessarily all of the pieces preceeding a <br /> element. Consider foo2a in the following example.

#!/usr/bin/perl use HTML::TreeBuilder::XPath; use strict; use warnings; my $root = HTML::TreeBuilder::XPath->new; $root->parse_file(\*DATA) or die("Could not parse the data: $!\n"); $root->eof(); my $xpath = '//div/p'; for my $d ($root->findnodes($xpath)) { my @line = $d->content_list; s/^\s+|\s+$//g for @line; $d->replace_with($line[0],qq(\n)); } print $root->as_trimmed_text,qq(\n); $root->delete; exit(0); __DATA__ <div><p>foo00 bar00</p></div> <div><p>foo01<br />bar01</p></div> <div> <p> foo02 <br /> bar02 </p> </div> <div> <p> <a href="foobar01">foobar02</a> foo02a <br /> bar02a </p> </div> <div> <p> foo03 <br /> bar03 <br /> baz03 </p> </div> <div> <p> <em>foo04</em> <br /> <strong>bar04</strong> <br /> <em>baz04</em> </p> </div> <div> <p> <em>foo05</em> </p> <p>bar05 <br /> <em>baz05</em> </p> </div>

I am trying many experiments with $d->content_list. I suppose it would be possible to extract the node as a hash and then loop through it eliminating the <br /> element and everything after it.

Replies are listed 'Best First'.
Re^5: Truncating an HTML node using XPaths in HTML::TreeBuilder::XPath
by tangent (Parson) on Sep 28, 2019 at 19:20 UTC
    You can test each item in content_list to see if it is a <br> tag:
    for my $d ($root->findnodes($xpath)) { my @line = $d->content_list; my @truncated; for my $line (@line) { if (ref $line) { last if $line->tag eq 'br'; } push(@truncated,$line); } $d->replace_with(@truncated); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106798]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-28 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found