2011-06-22 60 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; 
} 

对不起,我没有测试这段代码。