这个是CU上斑竹提供的例子,
使用AnyEvent::HTTP模块并行异步抓取网页
写的确实结构很清晰,可以作为经典例子
#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::HTTP;
my $cocurrent = 100; # 并发数
my @todoList = map { "keyword" . $_ } (1 .. 1000); # 待查询的关键词
my $cv = AnyEvent->condvar;
doit() foreach 1..$cocurrent;
sub doit{
my $word = shift @todoList;
return if not defined $word;
$cv->begin;
http_get( "http://www.baidu.com/s?wd=$word", sub { done( $word, @_ ) } );
}
sub done {
my ($word, $content, $hdr) = @_;
$cv->end();
print "Search: $word\tStatus: ", $hdr->{Status}, "\n";
doit();
}
$cv->recv();

本文详细介绍了如何利用Perl语言中的AnyEvent::HTTP模块实现并行异步网页抓取,通过设置并发数、构建待查询关键词列表及定义回调函数,有效提升了数据获取效率。
648

被折叠的 条评论
为什么被折叠?



