7stud has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
When I parse a string of html that I conjured up looking for span tags that have the attribute class="value", this code works:
use strict; use warnings; use 5.010; use LWP::Simple; use HTML::TreeBuilder; my $html =<<'END_HTML'; <html> <head><title></title></head> <body> <div><span class="value">Hi</span></div> <p>Thanks</p> <div><span class="value">Hello</span></div> </body> </html> END_HTML my $tree = HTML::TreeBuilder->new_from_content($html); my @spans = $tree->look_down(class => 'value'); for my $span (@spans) { say $span->as_trimmed_text(); } $tree->delete(); --output:-- Hi Hello
But when I unleash my code in the wild, it doesn't find the span tags:
use strict; use warnings; use 5.010; use LWP::Simple; use HTML::TreeBuilder; my $url = 'http://www.almanac.com/weather/history/zipcode/21218/2008-0 +9-02'; my $html = get($url); my $tree = HTML::TreeBuilder->new(); $tree->parse_file($html); my $span_tag = $tree->look_down( class => 'value', ); say $span_tag->as_trimmed_text(); $tree->delete(); --output:-- Can't call method "as_trimmed_text" on an undefined value at 1perl.pl +line 41.
Can anyone see what the problem is?
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTM::TreeBuilder, HTML::Element's look_down() method
by afresh1 (Hermit) on Oct 06, 2010 at 21:11 UTC | |
by 7stud (Deacon) on Oct 06, 2010 at 21:24 UTC | |
by afresh1 (Hermit) on Oct 06, 2010 at 21:45 UTC |