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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|