使用WWW :: Mechanize查找包含粗体文本的链接

时间:2014-10-14 05:34:20

标签: perl mechanize

假设HTML页面的内容是

<a href="abc.com"><b>ABC</b>industry</a>
<a href="google.com">ABC Search</a>
<a href="abc.com">Movies with<b>ABC</b></a>

我想只提取包含粗体文本的链接。我怎样才能使用WWW :: Mechanize?

输出

ABC industry
Movies with ABC

我用过

@arr=$m->links();
foreach(@arr){print $_->text;}

但是这会找到页面中的所有网址。

1 个答案:

答案 0 :(得分:2)

如果不使用可以解析页面内容的额外模块,使用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一样),并且可以使用$ua->get($url)->res->dom访问返回页面的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文档中有很多例子!

要获得有用的8分钟介绍性视频Mojo::DOMMojo::UserAgent,请查看Mojocast Episode 5