繁体   English   中英

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

[英]find links containing bold text using WWW::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;}

但这会找到页面中的所有URL。

如果不使用额外的模块来解析页面内容,将很难通过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访问返回页面的$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::DOMMojo::UserAgent的有用的8分钟入门视频,请查看Mojocast Episode 5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM