简体   繁体   中英

How can I get the links from a web page using WWW::Mechanize and Perl

I'm new to Perl and trying to get all the links on a website that begin with http:// , so far the code I have written returns values that are in hex. I would like to see the actual links it is returning. I have posted my code below, any and all inputs will be greatly appreciated.

use strict;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();

my $url = "http://search.cpan.org";

$mech->get($url);

my @foundLinks = $mech->find_all_links();
print @foundLinks; 

From the documentation :

$mech->find_all_links( ... )

Returns all the links on the current page that match the criteria. The method for specifying link criteria is the same as in "find_link()". Each of the links returned is a WWW::Mechanize::Link object.

To print the links, use the url method to WWW::Mechanize::Link :

my @foundLinks = $mech->find_all_links();
print $_->url for @foundLinks;

find_all_links的返回值是WWW :: Mechanize :: Link对象,因此您可以这样做

 print map {$_->url} @foundlinks;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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