免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 墨迹哥
打印 上一主题 下一主题

Perl爬虫,爬整站研究 [复制链接]

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
11 [报告]
发表于 2013-06-02 00:50 |只看该作者
回复 7# iakuf


    感谢。。上次帖子太乱真的没注意到。。今天看到你写的代码,上次那个我也测试了,但是上次那个代码我记得也是只能抓取一级页面啊?
    我希望的是可以抓取2级甚至三级的页面,这对我来说很重要。爬虫的效率直接影响后续的操作啊。。
    MOjo的模块我还真没有研究过。。我好好看看。。之前看到你博客的文章了。确实,你一开始写的那个,我就有关注,后续感觉有点纠结,
    因为没看懂你的代码,这两天我会好好研究下,如果可以的话欢迎加入一起讨论爬虫。目前那鸟人也在写。 不知道写到哪了。。
    我自己写了个只能抓一级页面,也很蛋腾。云舒的那个代码不知道你看过木有?它那个能够抓整站的目的。但是貌似有BUG,在抓取数量到达
    10000个URL的时候直接代码异常了。。- - ! 纠结ING!

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
12 [报告]
发表于 2013-06-02 00:53 |只看该作者
回复 4# iakuf


    对了,这个capacity => 100000如果超了就会出异常,这个怎么处理?

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
13 [报告]
发表于 2013-06-02 00:54 |只看该作者
回复 2# mcshell


    你又开始缩到一个点去。。。你也加入一起讨论撒。。你最近研究什么去了。怎么老是不见你说话。。
    - - !是不是去上生理辅导课去了?。。。

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
14 [报告]
发表于 2013-06-02 00:57 |只看该作者
回复 7# iakuf


    抓163的时候就出现问题了。。。
  1. 25331 3 http://reg.163.com/reg/reg.jsp?product=urs
  2. 25332 3 /otp/controller/index.jsp
  3. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  4. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  5. Use of uninitialized value $host in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 248.
  6. 25333 3 /mibao/mpp/zht.jsp
  7. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  8. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  9. Use of uninitialized value $host in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 248.
  10. 25334 3 /mibao/controller/phone/index.jsp
  11. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  12. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  13. Use of uninitialized value $host in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 248.
  14. 25335 3 /mibao/controller/ppc/index.jsp
  15. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  16. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  17. Use of uninitialized value $host in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 248.
  18. 25336 3 http://reg.163.com/lockuser/LockUserByPingma_0.jsp
  19. 25337 3 http://reg.163.com/help/help_oauth2.html
  20. 25338 3 http://piao.163.com/shanghai/movie/1643.html#from=hot01.pic
  21. 25339 3 http://piao.163.com/guangzhou/movie/43454.html#from=hot09.pic
  22. 25340 3 http://piao.163.com/guangzhou/movie/1603.html#from=coming01.pic&tab=comment
  23. 25341 3 http://piao.163.com/guangzhou/movie/1642.html#from=banner1
  24. 25342 3 http://yuehui.163.com/viewuser.do?id=<#=uinfo.id#>
  25. 25343 3 http://yuehui.163.com/viewdate.do?id=<#=dinfo.did#>
  26. 25344 3 http://yuehui.163.com/searchdate.do
  27. 25345 3 /login.do
  28. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  29. Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
  30. Use of uninitialized value $host in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 248.
  31. 25346 3 http://fashion.163.com/photoview/43AJ0026/44338.html?from=htp
  32. 25347 3 http://fashion.163.com/photoview/25A20026/44334.html?from=htp
  33. 25348 3 http://comment.lady.163.com/photoview_bbs/PHOT1B9D002643AJ.html
  34. 25349 3 http://fashion.163.com/photoview/43AJ0026/44331.html
  35. 25350 3 http://fashion.163.com/photoview/43AJ0026/44337.html
  36. 25351 3 #p=902888GO43AJ0026
  37. 25352 3 #p=902888U543AJ0026
  38. 25353 3 #p=902888Q043AJ0026
  39. 25354 3 #p=902888P543AJ0026
  40. 25355 3 http://fashion.163.com/photoview/43AJ0026/44338.html
  41. 25356 3 http://fashion.163.com/photoview/25A20026/44334.html
  42. 25357 3 http://fashion.163.com/photoview/25A20026/44332.html
  43. 25358 3 http://fashion.163.com/photoview/25A20026/44323.html
复制代码

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
15 [报告]
发表于 2013-06-02 00:59 |只看该作者
回复 5# dugu072


    请教兄台,对于你说的问题,是否有好的办法解决呢?不妨指教一二?

论坛徽章:
1
辰龙
日期:2014-05-15 19:37:15
16 [报告]
发表于 2013-06-02 20:14 |只看该作者
回复 9# dugu072

你如果是要控制的事件的数量,你想你需要的参数是 Mojo::Loop 中修改 max_accepts 和 max_connections 的属性,都有完整的接口。另外,考虑下使用 libev 会性能好多了,所以装个 EV 的模块相当有必要。会自动帮你调用的。


   

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
17 [报告]
发表于 2013-06-02 20:40 |只看该作者
  1. #!C:\Perl\bin\perl.exe
  2. use strict;
  3. use LWP::UserAgent;
  4. use HTTP::Request::Common qw(GET);

  5. my $url = "http://www.chinaunix.net/";
  6. my $base_url = "chinaunix";
  7. my @exclude = ("thread","uid-","uid/","forum-","Start_","forummodule-","fid=","search.php","=","download/","css","js","news","shtml","peixun","page-");#url排除

  8. my @storeurl;
  9. my @newurl;
  10. my @findurl;
  11. my @waitfindurl;
  12. my $temp=0;

  13. push @waitfindurl,$url;
  14. push @storeurl,$url;
  15. for my $wfu (@waitfindurl){
  16.         #last if (@storeurl > 150);
  17.         print @storeurl ." ". @waitfindurl." $temp $wfu\n";
  18.         @findurl = ();
  19.         @findurl = &findpageurl($wfu,$base_url,\@exclude);
  20.         next unless ($findurl[0] =~ /http/i);        #返回不时http就忽略
  21.        
  22.         for my $fu (@findurl){
  23.                 next if (map {$fu=~/^$_$/i} @storeurl);        #比对url库中是否存在,存在就舍弃
  24.                 next unless ($fu =~ /http/i);                        #剔除非http的链接
  25.                 push @storeurl,$fu;
  26.                 push @newurl,$fu;
  27.                 print @storeurl." ".@newurl." storeurl _ newurl \n";
  28.         }
  29.         push @waitfindurl,@newurl;
  30.         shift @waitfindurl;
  31.         @newurl = ();#清空新找到的url列表
  32.         $temp++;
  33. }

  34. open FF,">url.txt";
  35. print FF "$_\n" for(@storeurl);
  36. close FF;

  37. sub findpageurl{
  38.         my ($url,$base_url,$a) = @_;
  39.         my @exclude = @$a;
  40.         my $UA = LWP::UserAgent->new();
  41.         my $req = HTTP::Request->new( GET => "$url" );
  42.         my $resp = $UA->request($req);
  43.         my @page;
  44.         my @hrefurl;
  45.         my @srcurl;
  46.         my $return_code;
  47.         my $tempurl;

  48.         #链接不是200返回错误代码
  49.         $return_code = $resp->code;
  50.         print "requset return code:$return_code\n";
  51.         return $return_code unless ($return_code == 200);
  52.         @page = split /\n/,$resp->content;
  53.         for(@page){
  54.                 if(s/href="(.+?)"//g){
  55.                         $tempurl = $1;
  56.                         next if (map {$tempurl=~/$_/i} @exclude);
  57.                         substr($tempurl,-1) =~ s/\///;        #移除url最后的 /
  58.                         push @hrefurl,$tempurl if ($tempurl =~ /$base_url/i);
  59.                 }
  60.                 #push @srcurl,$1 if(/src="(.*?)"/g);
  61.         }
  62.         print "requset return url\n";
  63.         return @hrefurl;
  64. }
复制代码
我拿chinaunix做了测试,@exclude数组中会排除了站点中各种分页。下一步将更加更加细找到的url ,从而逐步去除@exclude这个数组
@waitfindurl中所有元素都检查过后脚本就结束。细细想下这个办法 因该还是有问题的

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
18 [报告]
发表于 2013-06-02 21:03 |只看该作者
回复 17# grshrd49


    还要排除分站,这点也很重要,不能抓取非本站的数据,这样会导致超出范围的问题,我主要还是不太理解那个bloo的那个模块里面容量的问题,
    一般大型网站都会超出容量,那它设置这个毫无意义。我就纳闷了。我也在写,等会把bug改完再发出来。

论坛徽章:
1
辰龙
日期:2014-05-15 19:37:15
19 [报告]
发表于 2013-06-02 22:56 |只看该作者
回复 12# 墨迹哥
要多大设置多大,比你存内存中做 hash 表节约内存多了,你就理解这个 Bloom::Filter 的模块是个节约内存的 hash ,只是需要定义一个需要映射多大个元素的键值对就好,占用的内存这个会固定。不象 hash 中存的 url 过多,很快不能使用了。

   

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
20 [报告]
发表于 2013-06-02 23:19 |只看该作者
回复 19# iakuf


    这东西不可控制的啊!小站点的话还能控制说大概是1-2W条URL,如果大点的站点10W条,100W条。。那咋弄?。。。
    有没有办法能解决这问题。你看下我给你回的那个帖子,错误的信息就是因为超出限定的范围导致的。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP