TreeBuilder Get embedded nodes

106 views Asked by At

Basically, I need to get the names and emails from all of these people in the HTML code.

<thead>
        <tr>
            <th scope="col" class="rgHeader" style="text-align:center;">Name</th><th scope="col" class="rgHeader" style="text-align:center;">Email Address</th><th scope="col" class="rgHeader" style="text-align:center;">School Phone</th>
        </tr>
    </thead><tbody>
    <tr class="rgRow" id="ctl00_ContentPlaceHolder1_rg_People_ctl00__0">
        <td>
                            Michael Bowen
                        </td><td>[email protected]</td><td>903-488-3671 ext3200</td>
    </tr><tr class="rgAltRow" id="ctl00_ContentPlaceHolder1_rg_People_ctl00__1">
        <td>
                            Christian Calixto
                        </td><td>[email protected]</td><td>903-488-3671 x 3430</td>
    </tr><tr class="rgRow" id="ctl00_ContentPlaceHolder1_rg_People_ctl00__2">
        <td>
                            Rachel Claxton
                        </td><td>[email protected]</td><td>903-488-3671 x 3450</td>
    </tr>
    </tbody>

</table><input id="ctl00_ContentPlaceHolder1_rg_People_ClientState" name="ctl00_ContentPlaceHolder1_rg_People_ClientState" type="hidden" autocomplete="off">    </div>


        <br>

I know how to use treebuilder with the nodes and such, and I'm using this code in some of my script.

    my ($file) = @_;
my $html = path($file)-> slurp;
my $tree = HTML::TreeBuilder->new_from_content($html);
my @nodes = $tree->look_down(_tag => 'input');
my $val;
foreach my $node (@nodes) {
    $val = $node->look_down('name', qr/\$txt_Website/)->attr('value');
}
return $val;

I was going to use the same code for this function, but I realized that I don't have much to search for, since the <td> tag is in so many other places in the script. I'm sure there's a better way to approach this problem, but I can't seem to find it.

LINK TO HTML CODE: http://pastebin.com/qLwu80ZW

MY CODE: https://pastebin.com/wGb0eXmM

Note: I did look on google as much as possible, but I'm not quite sure what I should search for.

1

There are 1 answers

8
Borodin On BEST ANSWER

The table element that encloses the data you need has a unique class rgMasterTable so you can search for that in look_down

I've written this to demonstrate. It pulls the HTML directly from your pastebin

use strict;
use warnings 'all';

use LWP::Simple 'get';
use HTML::TreeBuilder;

use constant URL => 'http://pastebin.com/raw/qLwu80ZW';

my $tree = HTML::TreeBuilder->new_from_content(get URL);

my ($table) = $tree->look_down(_tag => 'table', class => 'rgMasterTable');

for my $tr ( $table->look_down(_tag => 'tr') ) {

    next unless my @td = $tr->look_down(_tag => 'td');

    my ($name, $email) = map { $_->as_trimmed_text } @td[0,1];

    printf  "%-17s %s\n", $name, $email;
}

output

Michael Bowen     [email protected]
Christian Calixto [email protected]
Rachel Claxton    [email protected]