2014-05-12 67 views
0

提取風格標籤數據反正是有,我可以從使用Perl使用Perl

#!/usr/bin/perl 
use strict; 

my $HTML = <<"EOF"; 
    <HTML> 
    <head> 
    <style type='text/css'> 

    #yui-dt0-bdrow0 td{background:#CFF;} 

    #yui-dt0-bdrow1 td{background:#CFF;} 

    #yui-dt0-bdrow2 td{background:#CFF;} 

    </style> 
    </head> 
    </HTML> 
EOF 

我需要提取從上面的HTML代碼yui-dt0-bdrow0 td{background:#CFF;}信息的HTML頁面中提取的風格標籤數據。

我搜索了很多模塊,但沒有找到合適的模塊。除此之外,我沒有嘗試編寫任何代碼來提取信息

任何幫助表示讚賞。

回答

5

使用Mojo::DOM

樣品:

#!/usr/bin/perl 
use strict; 
use warnings; 
use Mojo::DOM; 
my $HTML = <<"EOF"; 
    <HTML> 
    <head> 
    <style type='text/css'> 

    #yui-dt0-bdrow0 td{background:#CFF;} 

    #yui-dt0-bdrow1 td{background:#CFF;} 

    #yui-dt0-bdrow2 td{background:#CFF;} 

    </style> 
    </head> 
    </HTML> 
EOF 
my $dom = Mojo::DOM->new($HTML); 
print $dom->find('style')->text; 

輸出

[email protected]:~/myscripts$ perl mojo.pl 


    #yui-dt0-bdrow0 td{background:#CFF;} 

    #yui-dt0-bdrow1 td{background:#CFF;} 

    #yui-dt0-bdrow2 td{background:#CFF;} 

現在,您可以篩選出所需的數據。

有關Mojo::DOMMojo::UserAgent A 8分鐘的視頻教程退房Mojocast Episode 5