Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Parsing HTML

by jackyliesam (Initiate)
on Jun 30, 2008 at 22:22 UTC ( #694841=perlquestion: print w/replies, xml ) Need Help??

jackyliesam has asked for the wisdom of the Perl Monks concerning the following question:

How would one go about parsing and extracting "Epicor" out of:
<span class="classification_indent">Advertiser:</span>&nbsp; Epicor AS </div> </td>
I have tried different things and couldnt come up with a solution, thank you in advance for all your help.

Replies are listed 'Best First'.
Re: Parsing HTML
by pc88mxer (Vicar) on Jun 30, 2008 at 23:10 UTC
Re: Parsing HTML
by Popcorn Dave (Abbot) on Jun 30, 2008 at 23:30 UTC
    Take a look at HTML::TokeParser. I've used that successfully in the past, but I'm going on the assumption that you're going to want to pull other things out of the HTML source besides just that one item.


    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

    I would love to change the world, but they won't give me the source code

Re: Parsing HTML
by wfsp (Abbot) on Jul 01, 2008 at 06:50 UTC
    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 &nbsp;.

    #!/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>&nbsp; Epicor AS </div> </td>
    *Epicor AS*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://694841]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2021-11-30 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?