Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Help with xpath and TreeBuilder

by Gangabass (Vicar)
on Jun 29, 2012 at 11:04 UTC ( #979098=note: print w/replies, xml ) Need Help??

in reply to Help with xpath and TreeBuilder

It's really easy:

#!/usr/bin/perl use Modern::Perl; use HTML::TreeBuilder::XPath; use Data::Dumper; local $/; my $html = <DATA>; my $tree = HTML::TreeBuilder::XPath->new_from_content($html); my @values = $tree->findvalues('/html/body/p/a'); say Dumper(@values); __DATA__ <html> <body> <p><a href="1.html">Link Text 1</a></p> <p><a href="2.html">Link Text 2</a></p> <p><a href="3.html">Link Text 3</a></p> <p><a href="4.html">Link Text 4</a></p> </body> </html>

Replies are listed 'Best First'.
Re^2: Help with xpath and TreeBuilder
by Anonymous Monk on Jun 29, 2012 at 12:09 UTC

    Its even easier :)

    my @xpaths = qw{ /html/body/p/a /html/body/p[2]/a /html/body/p[3]/a .... /html/body/p[66]/a }; my $allXpaths = join ' | ', @xpaths; my @values = $tree->findvalues;

    I don't know about other xpath interpreters, but treebuilder::xpath (and xsh ) allows this

    This query is probably faster

    /html/body/p[ position()=1 or position()=2 or position()=3 or position()=66 ]/a

    Or this one

    /html/body/p[ ( ( position() > 0 and position() < 4 ) or ( position()=66 ) ) ]/a

    Though this one won't work with your html  //a[ position()=4] because each //a is at  //a[ position() = 1] because each //a is the only (first) child of its parent ( p ) --- I guess now I know how position() works

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://979098]
[Eily]: marto you just need a longer cable :P
[Corion]: marto: Yeah, but I'm somewhat wary of installing random USB drivers downloaded from , Google Drive or whatever, so Linux wins there due to there being no conflicts and me just having to edit one text file in the worst case, to add the USB vendor
[hippo]: Long USB cable FTW.
[Corion]: Eily: I've thought of that, but I don't like running long cables through the appartment because sooner or later I'll trip over it, pulling at least one device off its stand :)
[marto]: hmm, may have to patch CPAN::Meta to move from search.cpan to metacpan in the META.json/yml files
[Corion]: marto: Heh - it seems that they plan to keep the links alive for a long time. But still, I plan on moving PM to use/generate the new links
[Corion]: And I think it's better to generate links to the new world instead of keeping the older links alive by generating new versions of them ;)
[marto]: yeah, I guess it's supposed to be a permanent redirect, but better to make the move where possible

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2018-05-23 09:22 GMT
Find Nodes?
    Voting Booth?