Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:26:46+00:00 2026-06-17T13:26:46+00:00

I am using the LWP::UserAgent , HTML::Selector::XPath and HTML::TreeBuilder::XPath modules to get the value

  • 0

I am using the LWP::UserAgent,
HTML::Selector::XPath and
HTML::TreeBuilder::XPath modules to get the value of the href attribute of the first YouTube video in a set of search results.

My code so far is:

use LWP::UserAgent;
use HTML::TreeBuilder::XPath;
use HTML::Selector::XPath;

my $ua = LWP::UserAgent->new;

#my $response =..
my $html = "http://www.youtube.com/results?search_query=run+flo+rida";

my $tree = HTML::TreeBuilder::XPath->new;

my $xpath = HTML::Selector::XPath::selector_to_xpath("(//*[@id = 'search-results']/li)[1]/div[2]/h3/a/@href/");
my @nodes = $tree->findnodes($xpath);
print" $nodes[0]";

I’m not sure if my printing is incorrect of if some other syntax is wrong. As of now it prints

HTML::TreeBuilder::XPath=HASH(0x1a78250)

when I am looking for it to print

/watch?v=JP68g3SYObU

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:26:47+00:00Added an answer on June 17, 2026 at 1:26 pm

    There are a number of problems here.

    • You must always use strict and use warnings at the top of every Perl program. It will catch many errors that you would easily overlook, and is only polite when you are asking for help with your code. In this case it would have warned you that your XPath string contained array variable names @id and @href which you may not have intended to be interpolated into the string.

    • You are using HTML::Selector::XPath, which translates a CSS selector to an XPath expression. But you are supplying it an XPath expression, so it will not work and the module is not needed.

    • There is no need to use LWP at all, as HTML::TreeBuilder has a new_from_url constructor which will fetch the HTML page for you.

    This program seems to do what you need. I have also added the URI module to derive an absolute URL from the relative one in the href attribute value.

    use strict;
    use warnings;
    
    use HTML::TreeBuilder::XPath;
    use URI;
    
    my $url = "http://www.youtube.com/results?search_query=run+flo+rida";
    
    my $tree = HTML::TreeBuilder::XPath->new_from_url($url);
    
    my $anchor = $tree->findnodes('//ol[@id="search-results"]//h3[@class="yt-lockup2-title"]/a/@href');
    my $href = URI->new_abs($anchor->[0]->getValue, $url);
    print $href;
    

    output

    http://www.youtube.com/watch?v=JP68g3SYObU
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following code: use LWP::Simple; my $url= http://example.com; my $html= get $url; I
I want to post russian text on a CP1251 site using LWP::UserAgent and get
I am using LWP to get html from three different web pages (defined dynamically)
When using the get method from LWP::UserAgent with :content_file and :content_cb set but without
I am having some issue here using LWP::Simple to get ftp site content in
I'm trying to get a html source of a webpage using the Perl "get"
I am fetching some pages over the Web using Perl's LWP::UserAgent and would like
our $ua = LWP::UserAgent->new; my $response = $ua->get($url); if($response->is_success) { my $perl_hash_or_arrayref = decode_json(encode(UTF-8,
I'm using LWP::UserAgent to request a lot of page content. I already know the
I am retrieving a ´ISO-8859-1´ encoded website by using ´LWP::UserAgent´ with the following code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.