如果不使用可以解析页面内容的额外模块,使用WWW::Mechanize 将很难实现您的目标。但是,还有其他模块可以让您轻松实现这一目标。
这里是一个使用Mojo::DOM 的示例,它可以让您像在 CSS 中一样选择元素。 Mojolicious 发行版还包含Mojo::UserAgent,因此如果您不太依赖WWW::Mechanize,您可以相当轻松地将代码迁移到Mojo。
# $html is the content of the page
my $dom = Mojo::DOM->new($html);
# extract all <b> elements that are under <a> elements (at any depth beneath the <a>)
# and get the <a> ancestors of those elements
# creates a Mojo::Collection object
my $collection = $dom->find('a b')->map(sub{ return $_->ancestors('a') } )->flatten;
$collection->each( sub {
say "LINK: " . $_->all_text;
} );
# Use a sub to perform an action on each of the retrieved <a> elements:
$dom->find('a b')->each( sub {
$_->ancestors('a')->each( sub {
say "All in one: " . $_->all_text
} )
} );
这是一个带有示例链接列表的演示:
<html>
<ul><li><a href="abc.com"><b>ABC</b> industry</a></li>
<li><a href="google.com">ABC Search</a></li>
<li>Here is <a href="#">a link
<span>with a span
<b>and a "b" tag</b>
even though
</span> "b" tags are deprecated.</a> Yay!</li>
<li><a href="abc.com">Movies with <b>ABC</b></a></li></ul></html>
输出:
LINK: ABC industry
LINK: a link with a span and a "b" tag even though "b" tags are deprecated.
LINK: Movies with ABC
All in one: ABC industry
All in one: a link with a span and a "b" tag even though "b" tags are deprecated.
All in one: Movies with ABC
如果您使用Mojo::UserAgent 而不是WWW::Mechanize,您的搜索会更容易。 Mojo::UserAgent可以get一个页面(就像WWW::Mechanize一样),返回页面的DOM可以使用$ua->get($url)->res->dom访问。然后,您可以在上面链接您的查询,以提供以下信息:
my $ua = Mojo::UserAgent->new();
# get the page and find the links with a <b> element in them:
$ua->get('http://my-url-here.com')
->res->dom('a b')->each( sub { $_->ancestors('a')->each( sub { say $_->all_text } ) } );
# example using this page:
# print the contents of divs with class 'spacer' that contain a link with a div in it:
$ua->get('http://stackoverflow.com/questions/26353298/find-links-containing-bold-text-using-wwwmechanize')
->res->dom('a div')->each( sub {
$_->ancestors('div.spacer')->each( sub {
say $_->all_text
} )
} );
输出:
1 How to use WWW::Mechanize to submit a form which isn't there in HTML?
0 How to process a simple loop in Perl's WWW::Mechanize?
0 Perl WWW::Mechanize cookie problem
1 Getting error in accessing a link using WWW::Mechanize
0 How to use output from WWW::Mechanize?
-2 Use WWW::Mechanize to login in webpage without form login but javascript using perl
3 Perl WWW::Mechanize Web Spider. How to find all links
0 Howto use WWW::Mechanize to access pages split by drop-down list
0 What is the best way to extract unique URLs and related link text via perl mechanize?
0 Perl WWW::Mechanize doesn't print results when reading input data from a data file
Mojolicious 文档中有很多示例,以防无法立即理解!
如需观看 Mojo::DOM 和 Mojo::UserAgent 的 8 分钟介绍视频,请查看 Mojocast Episode 5。