0

Below is my data in html file. I want to find the values in the html file using "HTML::TreeBuilder"

<table id="stats" cellpadding="0" cellspacing="0">
<tbody>
    <tr class="row-even">
        <td class="stats_left">Main Domain</td>
        <td class="stats_right"><b>myabcab.com</b></td>
    </tr>
    <tr class="row-odd">
        <td class="stats_left">Home Directory</td>
        <td class="stats_right">/home/abc</td>
    </tr>
    <tr class="row-even">
        <td class="stats_left">Last login from</td>
        <td class="stats_right">22.32.232.223&nbsp;</td>
    </tr>
    <tr class="row-odd">
        <td class="stats_left">Disk Space Usage</td>
        <td class="stats_right">30.2 / &#8734; MB<br>
        <div class="stats_progress_bar">
        <div class="cpanel_widget_progress_bar" title="0%"
            style="position: relative; width: 100%; height: 100%; padding: 0px; margin: 0px; border: 0px">
        </div>
        <div class="cpanel_widget_progress_bar_percent" style="display: none">0</div>
        </div>
        </td>
    </tr>
    <tr class="row-even">
        <td class="stats_left">Monthly Bandwidth Transfer</td>
        <td class="stats_right">0 / &#8734; MB<br>
        <div class="stats_progress_bar">
        <div class="cpanel_widget_progress_bar" title="0%"
            style="position: relative; width: 100%; height: 100%; padding: 0px; margin: 0px; border: 0px">
        </div>
        <div class="cpanel_widget_progress_bar_percent" style="display: none">0</div>
        </div>
        </td>
    </tr>
</tbody>
  </table>

How can I find "Disk Usage space" value using "HTML::TreeBuilder". I have many tds with same classes from above code,

4

1 回答 1

4

查找<td>具有匹配内容的 ,在本例中为“磁盘空间使用情况”,然后查找下一个<td>

一旦你有了一个元素树:

my $usage = $t->look_down(
    _tag => 'td',
    sub {
        $_[0]->as_trimmed_text() =~ /^Disk Space Usage$/
    }
)->right()->as_trimmed_text();

您可能希望将其包装在 eval 块中,以防look_down找不到匹配项。

HTML::Element 中的树导航方法是有效利用 HTML::TreeBuilder 的关键部分。


Mohini 问道:“为什么这不起作用?”

(我添加的格式)

use strict;
use warnings;
use HTML::TreeBuilder;

my $tree = HTML::TreeBuilder->new_from_file( "index.html");
my $disk_value; my $disk_space;

for ( $tree->look_down( _tag => q{tr}, 'class' => 'row-odd' ) ) {

    $disk_space = $tree->look_down(
         _tag => q{td},
         'class' => 'stats_left'
    )->as_trimmed_text;

    if ( $disk_space eq 'Home Directory' ) {
        $disk_value = $tree->look_down( _tag => q{td}, 'class' => 'stats_right' )
                           ->right()
                           ->as_trimmed_text();
    }

}

print STDERR "my home value is $disk_space : $disk_value\n";

look_down从您调用它的根节点开始,向下查看元素树(这些树倒置生长)并返回匹配节点列表或第一个匹配节点,具体取决于上下文。

由于所有向下查找的调用都在树上,因此您每次通过循环都会重复找到相同的节点。

您的循环应该看起来更像这样:

my %table_stuff;

for my $odd_row ( $tree->look_down( _tag => q{tr}, 'class' => 'row-odd' ) ) {

    $heading = $odd_row->look_down(
         _tag => q{td},
         'class' => 'stats_left'
    );

    $table_stuff{ $heading->as_trimmed_text() } = $heading->right()->as_trimmed_text();
}

这将使用表元素填充散列。

如果您只想要一个值,则根本不要使用循环。 look_down已经充当循环。

my $heading = $t->look_down(
    _tag => 'td',
    sub {
        $_[0]->as_trimmed_text() =~ /^Home Directory$/
    }
);

my $value = $heading->right();

#  Now $heading and $value have HTML::Element nodes that you can do whatever you want with.

my $disk_value = $value->as_trimmed_text();
my $disk_space = $heading->as_trimmed_text();
于 2012-09-27T07:41:07.010 回答