How can I extract URL and link text from HTML in Perl?

后端 未结 11 1660
一生所求
一生所求 2020-11-27 17:19

I previously asked how to do this in Groovy. However, now I\'m rewriting my app in Perl because of all the CPAN libraries.

If the page contained these links:

<
11条回答
  •  春和景丽
    2020-11-27 17:34

    Please look at using the WWW::Mechanize module for this. It will fetch your web pages for you, and then give you easy-to-work with lists of URLs.

    my $mech = WWW::Mechanize->new();
    $mech->get( $some_url );
    my @links = $mech->links();
    for my $link ( @links ) {
        printf "%s, %s\n", $link->text, $link->url;
    }
    

    Pretty simple, and if you're looking to navigate to other URLs on that page, it's even simpler.

    Mech is basically a browser in an object.

提交回复
热议问题