in reply to Re^2: PERL HTML::TableExtractor
in thread PERL HTML::TableExtractor
The table cells we are interested in look like the following (tidied up):
First we get an array of all those cells (an array of H::E objects)<td width="48%" valign="top"> <b>SERVPRO<sup><small>®</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>
In scalar context $obj->look_down returns the first found, in list context it returns all of them.my @cells = $r->look_down( _tag => q{td}, width => q{48%}, valign => q{top}, );
For each cell
we first look down for the the bold tag element and print out the text within itfor my $cell (@cells){
we then iterate over a list of the elementsmy $bold = $cell->look_down(_tag => q{b}); print $bold->as_text, 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.for my $item ($cell->content_refs_list) { next if ref $$item; print $$item, qq{\n}; }
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
and print out the href attributemy $link = $cell->look_down( _tag => q{a}, );
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..print $link->attr(q{href}), qq{\n\n};
You can get the low down on the arrow -> in perlreftut and perlref. We use it here to call an objects method.
Good luck!
|
|---|