2012-09-27 80 views
0

以下是我在html文件中的數據。我想找到在HTML文件中使用「HTML :: TreeBuilder作爲」使用HTML :: TreeBuilder查找html文件中的值

<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> 

我如何才能找到使用「HTML :: TreeBuilder作爲」值「磁盤使用空間」值。我有許多與上面的代碼相同的類的tds,

回答

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的關鍵部分。


莫希尼問:「爲什麼不工作?」

(格式化由我添加)

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(); 
+0

TreeBuilder是雙重有效! ;-) – simbabque

+0

@daotoad:我用下面的代碼whichis不工作。我不能做這樣的事情 使用嚴格;使用警告;使用HTML :: TreeBuilder; my $ tree = HTML :: TreeBuilder-> new_from_file(「index.html」); my $ disk_value; my $ disk_space;對於($ tree-> look_down(_tag => q {tr},'class'=>'row-odd')){ $ disk_space = $ tree-> look_down(_tag => q {td},' class'=>'stats_left') - > as_trimmed_text;如果($ disk_space eq'主目錄'){ \t $ disk_value = \t $ tree-> look_down(_tag => q {td},'class'=>'stats_right') - > right() - > as_trimmed_text ); \t } } print STDERR「my home value is $ disk_space:$ disk_value \ n」; – Cindrella

相關問題