设为首页 收藏本站
查看: 1077|回复: 0

[经验分享] perl多线程爬虫示例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-12-25 16:10:58 | 显示全部楼层 |阅读模式
  代码是从网上找的,注释是我加的



#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
use Thread::Queue;
use Thread::Semaphore;
use Bloom::Filter;
use URI;
use URI::URL;
use Web::Scraper;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;
#use HTTP::Cookies::Guess;
use String::Diff;
use String::Diff qw(diff_fully diff diff_merge diff_regexp);
use URI::Split qw(uri_split uri_join);
my $fid : shared;#下载的页面以递增的数字命名
share($fid);#多线程共享该变量
$fid=0;
#crawling with signed cookie
my $cookie_jar = '.mozilla/firefox/bg146ia6.default/cookies.sqlite';
my $tmp_ua = LWP::UserAgent->new;    #UserAgent用来发送网页访问请求
$tmp_ua->timeout(15);                ##连接超时时间设为15秒
$tmp_ua->protocols_allowed( [ 'http', 'https' ] ); ##只允许http和https协议
$tmp_ua->agent(
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;.NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
)
; ##用来在header中告诉服务器你用的是什么"浏览器",设置文件头的User-Agent
$tmp_ua->cookie_jar(HTTP::Cookies->new('file'=>"$ENV{'HOME'}/$cookie_jar",'autosave'=>1));
# 设置cookie,在运行过程中必须执行两个方法,extract_cookies($request) 和 add_cookie_header($response)。在运行的过程中实际用到了HTTP::Cookies模块。如:
# $ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });
# 等价于
# require HTTP::Cookies;
# $ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
push @{$tmp_ua->requests_redirectable}, 'POST';#告诉LWP在POST请求发送后如果发生重新定向就自动跟随
my $max_threads = 5;
my $base_url = $ARGV[0] || 'http://www.cnblogs.com/zhangchaoyang/';
my $host = URI::URL->new($base_url)->host;
print "Host Name: $host.\n";
my $queue = Thread::Queue->new( );#线程队列,每个线程负责去处理一个url
my $semaphore = Thread::Semaphore->new( $max_threads );
my $mutex = Thread::Semaphore->new( 1 );

#my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
#my $logfile = "crawler".($year+1900).($mon+1).$mday.$hour.$min.$sec.".log";
#open(BANLOG,">>$logfile") or die("can't open logfile:$!\n");
# Bloom::Filter使用更少的内存采用一种基于概率的算法来进行存在性测试。
my $filter = shared_clone( Bloom::Filter->new(capacity => 1000000, error_rate => 0.001) );
$queue->enqueue( $base_url );#放入线程队列的URL就要被线程所处理
$filter->add( $base_url );#放入filter中好判断该URL是否已经存在
my @tmp_url = ();#@tmp_url存在处理过的url
push(@tmp_url,$base_url);
while( 1 )
{
# join所有可以被join的线程
#my $joined = 0;
foreach ( threads->list(threads::joinable) )
{
#$joined ++;
$_->join( );
}
#print $joined, " joinedn";
# if there are no url need process.
my $item = $queue->pending();#返回队列中url的个数
# 线程队列为空
if( $item == 0 )
{
my $active = threads->list(threads::running);
# 已经没有active线程了,结束所有的工作
if( $active == 0 )
{
print "All done!\n";
last;
}
# 如果还有活动线程,那么主线程sleep,等待处理URL的子线程结束
else
{
#print "[MAIN] 0 URL, but $active active threadn";
sleep 1;
next;
}
}
# 线程队列不为空,信号量减1,占用一个线程来处理url
#print "[MAIN] $item URLn";
$semaphore->down;
#print "[MAIN]Create thread.n";
threads->create( \&ProcessUrl );
}
# join all threads which can be joined
foreach ( threads->list() )
{
$_->join( );
}
sub ProcessUrl
{
my $scraper = scraper
{
process '//a', 'links[]' => '@href';#根据XPath表达式寻找所有的标签a,把href属性存到散列的value中
};
my $res;
my $link;
while( my $url = $queue->dequeue_nb() )
{
eval#eval BLOCK,BLOCK只会被解析一次,并且在编译时进行代码语法检查。
{
print "开始下载",URI->new($url)->as_string,"\t\$fid=$fid\n";
LWP::Simple::getstore(URI->new($url)->as_string,"$ENV{'HOME'}/master/cnblog/cn$fid") or print "Can't download the web page.";
$fid+=1;
$scraper->user_agent($tmp_ua);#设置$scraper的user_agent
$res = $scraper->scrape( URI->new($url) )->{'links'};#把URI传给scrape函数。scrape函数返回一个数组引用,因为links是数组
};
if( $@ )# 当BLOCK中有语法错误、运行时错误遇到 die 语句, eval 将返回 undef 。错误码被保存在 $@ 中。
{
warn "$@\n";
next;
}
next if (! defined $res );#如果HTML文档中没有发现a标签
#print "there are ".scalar(threads->list(threads::running))." threads, ", $queue->pending(), " urls need process.n";
foreach( @{$res} )
{
# $_ => URI->new("http://example.com/")     所以要调用sa_string来获取"http://example.com/"
$link = $_->as_string;
$link = URI::URL->new($link, $url);
#$u1 = URI::URL->new($str, $base);
#$u2 = $u1->abs;
# not http and not https?
next if( $link->scheme ne 'http' && $link->scheme ne 'https' );
#The three forms of URI reference syntax are summarized as follows:
#<scheme>:<scheme-specific-part>#<fragment>
#<scheme>://<authority><path>?<query>#<fragment>
#<path>?<query>#<fragment>
#可以通过URL::Split把名个部分分离出来
# another domain?
# next if( $link->host ne $host );
#search for the sub domain
next if(!($link->host =~ /$host/));
$link = $link->abs->as_string;#获得绝对路径
if( $link =~ /(.*?)#(.*)/ )#去除书签锚点,即#以后的内容
{
$link = $1;
}
next if( $link =~ /rss|.(jpg|png|bmp|mp3|wma|wmv|gz|zip|rar|iso|pdf)$/i );#这些文件格式我们不抓取

#print "test:$link\n";
#EscapeUrl,skip query form values
my $tmp_link = &EscapeUrl($link);#$tmp_link中已经把查询参数的值去掉了
#print "Escape:".$tmp_link."\n";
$mutex->down(); #互质体减1,进入线程临界资源区
my $tmp_mark = 0;
#print "test start:$link\n";
if( ! $filter->check($tmp_link) )#如果$tmp_link不在$filter中
{
#print "Test filter ok:$tmp_link\n";
#DiffUrl,diff $link from queue with number
foreach(@tmp_url)
{
#print "Test Queue:".$tmpurl."\n";
#print "test-1:$_\ntest-2:$tmp_link\n";
if(&DiffUrl($_,$link))#如果发现@tmp_url中的url和当前页面中的一个链接url仅是在某些数字上不同(很可能是查询参数值不同),则跳过该链接,即跳到else里面去。
{
$tmp_mark = 2;
last;
}
}
if( $tmp_mark != 2 )
{
$queue->enqueue($link);#把页面上的链接$link交给线程进行处理
#print "add queue:$link\n";
$filter->add($tmp_link);#$tmp_link放入$filter
#print "add filter:$tmp_link\n";
#print BANLOG $filter->key_count(), " ", $link, "\n";
#print $filter->key_count(), " ", $link, "\n";
push(@tmp_url,$link);#把$link放入已处理的url数组@tmp_url
}
else
{
#print "pass:$link\n";#$link被忽略
}
}
#print "pass:$link\n";
$mutex->up();#互斥信号量加1
undef $link;
}
undef $res;#清除创建的一些object,否则在while循环中这些object越积越多
}
undef $scraper;
$semaphore->up( );##普通信号量加1
}
#close(BANLOG);
print "ALL DONE.\n";
#把URL尾部的request参数置为空
#比如http://category.dangdang.com/?ref=www-0-C&name=orisun-zhang#ref=www-0-C被处理为http://category.dangdang.com/?ref=&name=
sub EscapeUrl
{
my $urlold = shift;
my ($scheme,$auth,$path,$query,$frag) = uri_split($urlold);#把一个url的各部分分离出来
my $urlnew = uri_join($scheme,$auth,$path);
my $u = URI->new($urlold);
my @tmp_array = $u->query_form();
my $tmp = '';
my $i = 0;
for($i=0;$i<@tmp_array;$i+=2)#把request参数的值去掉
{
$tmp .=$tmp_array[$i]."=&";
}
if(@tmp_array != 0)
{
$tmp =~ s/&$//;
$urlnew .= "?".$tmp;
}
undef $u;#清除子例程中创建的object
#print $urlnew."\n";
return $urlnew;
}
sub DiffUrl
{
my $urlold = shift;
my $urlnew = shift;
my $urloldx = &EscapeUrl($urlold);
my $urlnewx = &EscapeUrl($urlnew);
my($old,$new) = String::Diff::diff($urloldx,$urlnewx);
#my($old,$new) = String::Diff::diff($urlold,$urlnew);
if (($old =~ m/(\[\d+\])/i) && ($new =~ m/{\d+}/i))#如果两个url仅是在某些数字上不同
#if ($new =~ m/{\d+}/i)
{
#print "test num success.\n";
return 1;
}
else
{
#print "test num failed.\n";
return 0;
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156326-1-1.html 上篇帖子: Perl 语言入门 (第五版) 下篇帖子: The ABC of Perl : My First Perl Program
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表