1
這裏是一個網頁我的樣本數據使用WWW ::在Perl模塊機械化
<hr>
<h4>This is Second line</h4>
Some Text Here<br>
Some More Text Here<br>
<h4>This is First line</h4>
Mem Capacity : 130.65 MB<br>
Mem Used : 74.52 MB<br>
Mem Available : 56.13 MB<br>
Mem Used Percentage : 57<br>
我使用下面的代碼來提取之類的東西
Mem Capacity : 130.65 MB
Mem Used : 74.52 MB
Mem Available : 56.13 MB
Mem Used Percentage : 57
的從網頁中提取線代碼是這樣的:
#!/usr/bin/perl
use WWW::Mechanize;
$mech = WWW::Mechanize->new();
$url = 'some url';
$result = $mech->get($url);
$content = $result->as_string();
print $content;
if($content =~ /Mem Capacity :([\d.]+)/)
{
$value = $1;
print "Memory Capacity $value MB n";
}
我沒有得到任何輸出。任何人都可以告訴我,我哪裏錯了?
您應該'使用嚴格的;使用警告;'。在這種情況下,我還會建議使用HTML解析器而不是WWW :: Mechanize,如果您只有HTML解決方案。 – squiguy
@squiguy你能告訴我怎樣才能用HTML解析器實現相同的代碼? – UKR
這顯然是從這個問題的延續:http://stackoverflow.com/questions/14051191/how-do-i-parse-html-with-perl和我的問題仍然是相同的:你如何定義行您正在尋找? –