免费注册 查看新帖 |

Chinaunix

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

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

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


    {:2_167:}  - - !我才知道。。

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


   
    避免回路出现死循环。使用队列来存储待访问的URL方法简单,网页访问顺序是固定死的;采用来存储待访问的URL,可以很好地用树的层次结构来表示网页的层次结构,对树的遍历可以采用前根遍历,也可以采用层次遍历。
    大量的页面都是我们不需要的--不是当当网图书详情页面,如何少做一些无用功。
    访问页面连接超时时间的设定。如果你网速不好,就设长一点,但是再次提醒你访问的大多数网页都是我们不需要的。

    这个和你说的应该是一致的对吗?

论坛徽章:
1
辰龙
日期:2014-05-15 19:37:15
33 [报告]
发表于 2013-06-03 10:51 |只看该作者
回复 29# 墨迹哥

25331 3 http://reg.163.com/reg/reg.jsp?product=urs
25332 3 /otp/controller/index.jsp
Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 59.
Use of uninitialized value $host in concatenation (.) or string at /usr/local/share/perl/5.14.2/Mojo/UserAgent.pm line 248.

基础语法多看看,英文多看看,这是有东西没有定义,因为找到的 url 有些没有 host 之类。和那个模块一点关系也没。


   

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
34 [报告]
发表于 2013-06-03 11:01 |只看该作者
回复 30# 墨迹哥
  1. D:\Program Files\Perl_workspace\网站爬虫>perl otherfind1.pl http://www.chinaunix.net/
  2. mainweb=http://www.chinaunix.net/
  3. domain is: chinaunix.net
  4. webaddress: http://www.chinaunix.net/
  5. getting the 1 th web of 1 in total
  6. successed!
  7. No such file or directory

  8. D:\Program Files\Perl_workspace\网站爬虫>perl otherfind1.pl www.chinaunix.net
  9. mainweb=www.chinaunix.net
  10. wrong input

  11. D:\Program Files\Perl_workspace\网站爬虫>
复制代码
这个脚本测试结果啊
晕死爬chinaunix直接结束了啊

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
35 [报告]
发表于 2013-06-03 11:07 |只看该作者
回复 32# 墨迹哥


我在测试我自己的脚本时候 发现chinaunix上有大量的404和各种分页 还有500 还有没有返回状态值的页面
我的排除功能做的不好,基本通过url硬剔除.
下班后再看看有什么更好的办法

论坛徽章:
1
辰龙
日期:2014-05-15 19:37:15
36 [报告]
发表于 2013-06-03 14:13 |只看该作者
回复 5# dugu072
相对路径的问题解决了,还修复了一些小 bug.
  1. #!/usr/bin/perl
  2. use strict;
  3. use Mojo::UserAgent;
  4. use Bloom::Filter;
  5. use Smart::Comments;

  6. my $dept_level = 2;
  7. my $baseUrl = Mojo::URL->new($ARGV[0] || 'http://www.chinaunix.net');
  8. my ($domain) = $baseUrl =~ qr#http://(?:www.)?([^/]+)#;
  9. my $filter = Bloom::Filter->new(capacity => 100000, error_rate => 0.0001);
  10. my $ua = Mojo::UserAgent->new(max_redirects => 3);

  11. my $callback;$callback = sub  {
  12.     my ($ua, $tx) = @_;
  13.     return if !$tx->success;

  14.     my $dept = $tx->req->headers->header('dept');
  15.     return if $dept > $dept_level; # 深度
  16.     ++$dept;
  17.     $tx->res->dom->find("a[href]")->each(sub{
  18.             my $attrs  = shift->attrs;
  19.             my $newUrl = Mojo::URL->new($attrs->{href});

  20.             # 修复 url 的路径
  21.             if (!$newUrl->host and !$newUrl->scheme) {
  22.                 $newUrl->host($tx->req->url->host);
  23.                 $newUrl->scheme($tx->req->url->scheme);
  24.             }   
  25.             $newUrl->fragment(undef); # 去掉 foo=bar#23 后面的 #xxx

  26.             # 域名, 协议, 后缀以下不对的都退出
  27.             next if ( $newUrl->scheme ne 'http' && $newUrl->scheme ne 'https' );
  28.             next if $newUrl->host !~ qr/$domain/;
  29.             next if ( $newUrl->path =~ /.(jpg|png|bmp|mp3|wma|wmv|gz|zip|rar|iso|pdf)$/i );

  30.             if( !$filter->check($newUrl) ) {
  31.                 print $filter->key_count(), " $dept ", $newUrl, "\n";
  32.                 $filter->add($newUrl);
  33.                 $ua->get($newUrl => { dept => $dept } => $callback);
  34.             }   
  35.     });
  36. };

  37. $ua->get($baseUrl => { dept => 1} => $callback);
  38. Mojo::IOLoop->start;
复制代码

论坛徽章:
0
37 [报告]
发表于 2013-06-03 15:08 |只看该作者
云总来了。

论坛徽章:
1
辰龙
日期:2014-05-15 19:37:15
38 [报告]
发表于 2013-06-03 15:17 |只看该作者
回复 37# 撒哈拉里的鱼

哈哈。。。。好不容易引出来啊。


   

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
39 [报告]
发表于 2013-06-03 15:50 |只看该作者
测试测试!
找个有正确答案的网站测试一下啊
是驴是马一测遍知

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


    {:2_169:}   你知道我英语一致都不怎么好。。。最近才报了华尔街的培训正在加强。。剩下你懂的。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP