I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2007-07-19 03:20:21-07 by yeddish
I'm having a little problem
I really love the functionality behind this package. This is what I have been looking for.
The problem that I am having is that anytime I try to access an object any deeper than /html/body it doesn't work. The following code always prints "Doesn't exist":

use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new; $tree->parse_file( "http://moneycentral.msn.com/investor/invsub/results/compare.asp?Page=TenYearSum +mary&Symbol=WHT"); $xpath = '/html/body/div[@id="wrapper"]'; if ($tree->exists($xpath)) { print "$xpath: Exists!\n"; } else { print "$xpath: Doesn't exist!\n"; }

If you could tell me what I am doing wrong it would be greatly appreciated.

Thanks.
-Joel
Direct Responses: 5752 | Write a response
Posted on 2007-07-19 05:27:22-07 by mirod in response to 5751
Re: I'm having a little problem

parse_file doesn't do what you think it does. Instead it does what it says, it parses a file ;--). So HTML::TreeBuilder::XPath looks for a file named 'http://moneycentral.msn.com/investor/invsub/results/compare.asp?Page=TenYearSum' and doesn't find it, hence returning a quasi empty tree (the html, head and body elements).

You need to load the HTML yourself, for example by using LWP::Simple, and doing this:

tree->parse_file( get "http://moneycentral.msn.com/investor/invsub/results/compare.asp?Page=TenYear +Summary&Symbol=WHT");

Does this help?

Direct Responses: 5755 | 5760 | Write a response
Posted on 2007-07-19 12:11:50-07 by yeddish in response to 5752
Re: I'm having a little problem
That helps greatly. Thank you very much!
-Joel
Direct Responses: Write a response
Posted on 2007-07-19 18:10:18-07 by yeddish in response to 5752
Re: I'm having a little problem
I have modified my code to include the changes that you mentioned but I still seem to get the same result. I just can't seem to figure out where my problem lies. Here's my new code:

use LWP::Simple; use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new; $html = get("http://moneycentral.msn.com/investor/invsub/results/compare.asp?Page=TenYearSummary&Sy +mbol=WHT"); $tree->parse_file($html); $xpath = '/html/body/div[@id="wrapper"]'; print "$html\n"; #This prints the HTML just fine if ($tree->exists($xpath)) { print "$xpath: Exists!\n"; } else { print "$xpath: Doesn't exist!\n"; }

Thanks.
-Joel
Direct Responses: 5771 | Write a response
Posted on 2007-07-20 05:15:43-07 by mirod in response to 5760
Re: I'm having a little problem

parse_file doesn't do what you think, but, as usual, what it says ;--) It parses a file, not a string. So this time it tries to parse a file with avery, very long name, full of pointy brackets, with the same result as your previous attempt. Replace parse_file by parse and you'll be all set.

A couple more things:
- what you should have printed, in order to check that the tree was properly created, is not $html, but $tree->as_HTML,
- always, ALWAYS, use strict, and most of the time use warnings, that will save you a lot aggravation when you develop

Direct Responses: 5773 | Write a response
Posted on 2007-07-20 12:34:59-07 by yeddish in response to 5771
Re: I'm having a little problem
Thank you very much. It's now working. Apparently, I'm more noob than I thought. :) -Joel
Direct Responses: Write a response