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 = "http://www.youtube.com/results?search_query=run+flo+rida"; 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
|
---|
In Section
Seekers of Perl Wisdom