Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

For this sort of 'Parse and extract' from HTML problem I reach for HTML::TreeBuilder. A first cut solution might look like:

use strict; use warnings; use HTML::TreeBuilder; my $str = do {local $/; <DATA>}; my $tree = HTML::TreeBuilder->new; $tree->parse ($str); my @jobs; for my $para ($tree->find ('p')) { my @class = $para->look_down ('class', 'jobname'); my @names = $para->look_down ('name', 'em'); my @offices = $para->look_down ('name', 'offices'); next unless @class && @names && @offices; my $job = $class[0]->as_text ();; $job .= ': ' . join '; ', map {$_->as_text ()} @names; $job .= ' (' . join (', ', map {$_->as_text ()} @offices) . ')'; push @jobs, $job; } print join "\n", @jobs; __DATA__ <p><b><span class="jobname">Sandbagger, Level 2</span> <span class="jobserial">(19000)</span><br /> Current members:<br /> <span name='em'>Fred</span><span name='em'>Wilma</span><br /> <span name='offices'>Erewon</span> </p> <p><b><span class="jobname">Accounting Assistant, Level 2</span> <span class="jobserial">(19203)</span><br /> Current members:<br /> <span name='em'>Plow, Elliot</span><span name='em'>Wang, Susan</span>< +br /> <span name='offices'>Huston</span> </p> <blockquote> Job descriptions here. This block quoted text contains a job description and it what I am rea +lly looking to recover. </blockquote> <blockquote><a href="#top">Go to the top of this page</a>.</blockquote +> <blockquote><a href='companyHR.html'>Check for open positions now!</a> +</blockquote>


Sandbagger, Level 2: Fred; Wilma (Erewon) Accounting Assistant, Level 2: Plow, Elliot; Wang, Susan (Huston)

I imagine that there might be lists of people fillin a particular role by office. This code will munge all of those entries together and list the offices, thus losing the association of people with offices. That can be fixed by using a $para->look_down ('name', qr/em|offices/) then iterating over the list spitting out employee names and office names as appropriate.

DWIM is Perl's answer to Gödel

In reply to Re: Parsing HTML files to recover data... by GrandFather
in thread Parsing HTML files to recover data... by UrbanHick

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2018-07-20 22:22 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (441 votes). Check out past polls.