2011-06-22 61 views
4

中的所有鏈接是否有方法通過使用Mechanize來查找特定div中的所有鏈接?Perl機械化查找Div

我試圖使用find_all_links,但無法找到解決方法。 例如,

<div class="sometag"> 
<ul class"tags"> 
<li><a href="/a.html">A</a></li> 
<li><a href="/b.html">B</a></li> 
</ul> 
</div> 
+1

什麼是你的參數find_all_links? – dwerner

回答

7

用於從HTML文件中獲取有用信息的有用工具是HTML::Grabber。它使用語法來引用元素在HTML的jQuery的風格,所以你可能會做這樣的事情:

use HTML::Grabber; 

# Your mechanize stuff here ... 

my $dom = HTML::Grabber->new(html => $mech->content); 

my @links; 
$dom->find('div.sometag a')->each(sub { 
    push @links, $_->attr('href'); 
}); 
1

網站::刮板刮有用。

use strict; 
use warnings; 
use WWW::Mechanize; 
use Web::Scraper; 

my $mech = WWW::Mechanize->new; 
$mech->env_proxy; 
# If you want to login, do it with mechanize. 

my $staff = scrape { process 'div.sometag li.tags a', 'links[]' => '@href' }; 
# pass mechanize to scraper as useragent. 
$staff->user_agent($mech); 

my $res = $staff->scrape(URI->new("http://example.com/")); 
for my $link (@{$res->{links}}) { 
    warn $link; 
} 

對不起,我沒有測試這段代碼。