Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: get data from xpath

by tobyink (Abbot)
on Jan 21, 2013 at 09:19 UTC ( #1014413=note: print w/replies, xml ) Need Help??

in reply to get data from xpath

A few problems:

  • You mention a URL, and you create a UA object, but you never actually use the UA to retrieve any HTML from the URL.

    Anyway, you don't need to use LWP::UserAgent directly because HTML::TreeBuilder::XPath supplies a new_from_url method.

  • You use HTML::Selector::XPath - this module is aimed at turning CSS-style selectors into XPath expressions, but you already have an XPath, so you don't need this module!

  • You're using @id and @href within double quotes - this will be interpreted as two Perl arrays! You want to single quote the XPath.

  • There shouldn't be a slash at the end of the XPath.

The following works:

use 5.010; use strict; use warnings; use HTML::TreeBuilder::XPath; my $url = ""; my $tree = HTML::TreeBuilder::XPath->new_from_url($url); my $xpath = '(//*[@id="search-results"]/li)[1]/div[2]/h3/a/@href'; my @nodes = $tree->findnodes($xpath); say $_->getValue for @nodes;
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014413]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-06-18 04:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.