好的,所以我知道如何在循环中使用Perl进行LWP get请求。但是我想知道是否有可能从网站上抓取一个Url并自动将其放入url中以循环访问?
#!usr/bin/perl
for ($i=0;$i<200;$i++)
{
use strict;
use LWP::UserAgent;
use warnings;
my $ua = new LWP::UserAgent(agent => 'USER AGENT');
my $response = $ua->get("http://example.com");
print $response->code,' ', $response->message,"\n";
sleep 2;
}
所以现在我想从一个域中抓取一个url并将其用于example.com,并且每次访问都会刮掉一个新的url。
答案 0 :(得分:4)
我想知道是否可以从网站上抓取一个Url并自动将其放入url中以循环访问?
烨!这称为队列,可以使用push
和shift
轻松实现。
my @to_visit = ( ...initial URLs... );
while (@to_visit) {
my $url = shift(@to_visit);
my $content = ...Download the file...;
my @extracted = ...Extract the URLs...;
push @to_visit, @extracted;
}
当然,您可能想要跳过您已访问过的网址。
my %seen;
my @to_visit = grep !$seen{$_}++, ( ...initial URLs... );
while (@to_visit) {
my $url = shift(@to_visit);
my $content = ...Download the file...;
my @extracted = ...Extract the URLs...;
push @to_visit, grep !$seen{$_}++, @extracted;
}