Here's my go using HTML::TokeParser::Simple. Rather a lot of it but I find it easy to write, debug and maintain. There's no need to try and cram everything into one loop, so I've used three. :-)
Note that the output is utf8 because of the .
#!/usr/local/bin/perl
use strict;
use warnings;
use HTML::Entities;
use HTML::TokeParser::Simple;
my $p = HTML::TokeParser::Simple->new(*DATA);
my $span_start;
while (my $t = $p->get_token){
$span_start++, last if (
$t->is_start_tag(q{span})
and
$t->get_attr(q{class})
and
$t->get_attr(q{class}) eq q{classification_indent}
);
}
die qq{start span not found\n} unless $span_start;
my $span_end;
while (my $t = $p->get_token){
$span_end++, last if $t->is_end_tag(q{span});
}
die qq{close span not found\n} unless $span_end;
my ($div_end, $encoded);
while (my $t = $p->get_token){
$div_end++, last if $t->is_end_tag(q{div});
$encoded .= $t->as_is if $t->is_text;
}
die qq{close div not found\n} unless $div_end;
die qq{no text found\n} unless $encoded;
my $decoded = decode_entities $encoded; # returns utf8
for ($decoded){
s/^\W+//;
s/\W+$//;
}
printf qq{*%s*\n}, $decoded;
__DATA__
<span class="classification_indent">Advertiser:</span>
Epicor AS
</div> </td>
*Epicor AS*
-
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.
|