首页 > 解决方案 > Perl While 循环 Http 和 HTTPS 响应

问题描述

$resp = $ab->request(HTTP::Request->new(GET => $url));
$rrs = $resp->content;



while(($rrs =~ m/<a href=\"https?:\/\/(.*?)\//g)  &&  ($rrs =~ m/<a href=\"?http:\/\/(.*?)\//g)){

标签: perl

解决方案


您的示例中断了,但看起来您想要获取资源,提取链接,并且可能做其他事情。我建议你让Mojolicious为你做这件事。它可以获取资源、解析 HTML ( dom)、提取其他链接 (in map),并选择具有正确方案的链接 (first grep):

use v5.10;

use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;

my @queue = ( $ARGV[0] );

my %Seen; # don't process things we've already seen
while( my $this = shift @queue ) {
    say "Processing $this";

    my $tx = $ua->get( $this );

    my @links = $tx->result
        ->dom
        ->find( 'a' )
        ->map( attr => 'href' )
        ->grep( sub { Mojo::URL->new($_)->scheme =~ /https?/ } )
        ->grep( sub { ! $Seen{$_} } )
        ->each;

    say "\t", join "\n\t", @links;

    push @queue, @links;
    }

我在Mojolicious Web Clients中写了很多例子。


推荐阅读