<?xml version="1.0" encoding="windows-1252"?>
<node id="1014413" title="Re: get data from xpath" created="2013-01-21 04:19:57" updated="2013-01-21 04:19:57">
<type id="11">
note</type>
<author id="757127">
tobyink</author>
<data>
<field name="doctext">
&lt;p&gt;A few problems:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You mention a URL, and you create a UA object, but you never actually use the UA to retrieve any HTML from the URL.&lt;/p&gt;
&lt;p&gt;Anyway, you don't need to use LWP::UserAgent directly because HTML::TreeBuilder::XPath supplies a &lt;c&gt;new_from_url&lt;/c&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You're using &lt;c&gt;@id&lt;/c&gt; and &lt;c&gt;@href&lt;/c&gt; within &lt;em&gt;double&lt;/em&gt; quotes - this will be interpreted as two Perl arrays! You want to &lt;em&gt;single&lt;/em&gt; quote the XPath.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There shouldn't be a slash at the end of the XPath.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following works:&lt;/p&gt;
&lt;c&gt;
use 5.010;
use strict;
use warnings;

use HTML::TreeBuilder::XPath;

my $url  = "http://www.youtube.com/results?search_query=run+flo+rida";
my $tree = HTML::TreeBuilder::XPath-&gt;new_from_url($url);

my $xpath = '(//*[@id="search-results"]/li)[1]/div[2]/h3/a/@href';
my @nodes = $tree-&gt;findnodes($xpath);

say $_-&gt;getValue for @nodes;
&lt;/c&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-757127"&gt;
&lt;small&gt;&lt;small&gt;&lt;tt&gt;package Cow { use Moo; has name =&gt; (is =&gt; 'lazy', default =&gt; sub { 'Mooington' }) } say Cow-&gt;new-&gt;name&lt;/tt&gt;&lt;/small&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1014379</field>
<field name="parent_node">
1014379</field>
</data>
</node>
