HTML::TreeBuilder builds trees of HTML::Elements. The methods we'll be looking at come from there. Keep the docs handy. :-)

The table cells we are interested in look like the following (tidied up):

<td width="48%" valign="top"> <b>SERVPRO<sup><small>&#174;</small></sup>of Northern Alabama</b> <br> Wilson, David & Christie <br> Phone: (205)678-2224 <br> Fax: (205)678-2226 <br> <a href='http://www.servpro.com/'>Visit their web site</a> </td>
First we get an array of all those cells (an array of H::E objects)
my @cells = $r->look_down( _tag => q{td}, width => q{48%}, valign => q{top}, );
In scalar context $obj->look_down returns the first found, in list context it returns all of them.

For each cell

for my $cell (@cells){
we first look down for the the bold tag element and print out the text within it
my $bold = $cell->look_down(_tag => q{b}); print $bold->as_text, qq{\n};
we then iterate over a list of the elements
for my $item ($cell->content_refs_list) { next if ref $$item; print $$item, qq{\n}; }
$obj->content_refs_list is another H::E method which, as you might guess, returns a list of references. Each reference is either a reference to an H::E object (i.e. another ref) or a reference to text. next if ref $$item; skips over other H::E objects (in this case the <b>, <br> and <a> tags) so what is left is a reference to text. $$item dereferences the reference.

In fact this looks very similar to the example in the H::E docs. So go see. :-)

Finaly we want to look down for the anchor tag

my $link = $cell->look_down( _tag => q{a}, );
and print out the href attribute
print $link->attr(q{href}), qq{\n\n};
Rather than print out the results you could push them onto an array (say, @record) so that $record[0] would be the location, $record[1] the phone number etc..

You can get the low down on the arrow -> in perlreftut and perlref. We use it here to call an objects method.

Good luck!


In reply to Re^3: PERL HTML::TableExtractor by wfsp
in thread PERL HTML::TableExtractor by jdlev

Title:
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, details, 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, summary, 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.