免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3536 | 回复: 8
打印 上一主题 下一主题

GoogleScanner [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-20 10:26 |只看该作者 |倒序浏览
本帖最后由 xti9er 于 2010-09-20 11:11 编辑

几年前写的旧代码,好久不用了,现在还能用。此代码既能能用来做好事也能用来做坏事,本来用来给老婆查论文资料的,结果她用不惯命令行的东西,就算了。做坏事就不用我说了吧。
配置文件如下格式

#########################

#=任意区域
#countryTD=乍得
#countryGI=直布罗陀
#countryCL=智利
#countryCF=中非共和国
#countryCN=中国
#countryMO=中国澳门特别行政区
#countryHK=中国香港特别行政区
#....等等
country=all
inurl:/index.htm

#########################
  1. #!/usr/bin/perl -w

  2. #By xti9er

  3. require LWP::UserAgent;
  4. use LWP::Simple;
  5. use Color::Output;
  6. Color::Output::Init;

  7. $|=1;

  8. #----------------------------
  9. #从配置文件中提取字符串
  10. #----------------------------
  11. my $inrulfile=shift||"inurl.ini";
  12. my $countryset="";
  13. open(INURL,$inrulfile) or die $!;
  14. while($inurl=<INURL>)
  15. {
  16.         chomp($inurl);
  17.         next if $inurl=~/^#/;
  18.         if($inurl=~/^country=(\w+)/i)
  19.         {
  20.            $countryset="country$1";
  21.         }
  22.         elsif($inurl=~/^country=$/i)
  23.         {

  24.         }
  25.         else{
  26.            push(@inurl,$inurl);
  27.         }
  28. }
  29. close INURL;

  30. for my $inurl(@inurl)
  31. {
  32.         $inurl=~s/\s/\+/g;
  33.         $reginurl=$inurl;
  34.         $reginurl=~s/\?/\\?/g;
  35.         $reginurl=~s/\=/\\=/g;
  36.         $reginurl=~s/\:/\\:/g;
  37.         $reginurl=~s/\+/\\s/g;

  38.         $sleeptime=5;

  39.         #cprin("Sleeptime=[$sleeptime]\t Url=[$inurl]\t RegUrl=[$reginurl]\t country=[$countryset]\n",7);
  40.         sleep(2);

  41.         my $sdco=0;
  42.         my $hostno=0;
  43.         my $cksd=0;
  44.         my $p75="-"x80;

  45.         my $ua = LWP::UserAgent->new;
  46.         $ua->timeout(20);
  47.         $ua->env_proxy;
  48.         $ua->agent("Mozilla/5.0");
  49.         #print "Start ...\n";

  50.         cprin("\t\t\t --=G o o g l e    \t S c a n n e r=-- \n",5);
  51.         cprin("\t\t\t                     By xti9er \n",13);
  52.         cprin("$p75\n",7);
  53.         $stime=time;
  54.         $lstime=localtime();
  55.         cprin("\t\t\tStart at $lstime\n",13);
  56.         my $searchweb="http://www.google.com.hk";
  57.         my $startpage=0;

  58.         print "[Google] page:$searchweb/search?num=100&complete=1&hl=zh-CN&cr=$countryset&newwindow=1&q=$inurl&start=0&sa=N/\n";

  59.         my $response = $ua->get("$searchweb/search?num=100&complete=1&hl=zh-CN&cr=$countryset&newwindow=1&q=$inurl&start=0&sa=N/")
  60.                  or (cprin("[Start] Get google start page faild:$!",5) and next);

  61.         if ($response->is_success)
  62.         {
  63.             $getre=$response->content;
  64.             @getlog=split(/href=/,$getre);
  65.             $getco=0;
  66.             for(@getlog)
  67.             {
  68.                 $getco++;

  69.                 if($_=~/id=resultStats>(.*)?</)
  70.                 {
  71.                     $ttpageno=$1;
  72.                     $ttpageno=~s/\,//g;
  73.                     $ttpageno=~s/\D//g;
  74.                     $ttpageno=int($ttpageno/100);
  75.                     cprin("\t\t\t$ttpageno Google Pages To Read!\n$p75\n",5);
  76.                     sleep(2);
  77.                     for($startpage=0;$startpage<=$ttpageno;$startpage=$startpage+100)
  78.                     {
  79.                         sleep($sleeptime);
  80.                         cprin("Now Read The ".$startpage."th Page!\n--------------------------------------\n",13);
  81.                         spider($startpage,$inurl);
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         else
  87.         {
  88.             cprin($response->status_line,5);next;
  89.         }

  90. }

  91. sub spider
  92. {
  93.     $pageno=shift;
  94.     my $inurl=shift;

  95.     sleep($sleeptime);
  96.     open(WLOG,"+>>$stime.log") or (cprin($!,5) and next);
  97.     my $searchweb="http://www.google.com.hk";
  98.     my $searchurl="$searchweb/search?num=100&complete=1&hl=zh-CN&cr=$countryset&newwindow=1&q=$inurl&start=$pageno&sa=N/";
  99.     my $nextpage=0;
  100.     my $nextpageno=$pageno+100;
  101.     cprin("Now URL: $searchurl\n",7);

  102.         my $ua = LWP::UserAgent->new;
  103.         $ua->timeout(20);
  104.         $ua->env_proxy;
  105.         $ua->agent("Mozilla/5.0");
  106.     my $response = $ua->get($searchurl);
  107.     if ($response->is_success)
  108.     {
  109.         $getre=$response->content; # or whatever
  110.         @getlog=split(/href=/,$getre);
  111.     }
  112.     else
  113.     {
  114.         print "Get page count faild!\n";
  115.         cprin($response->status_line,5);next;
  116.     }
  117.     my @sites;
  118.     foreach $urlre(@getlog){
  119.         if($urlre=~/^\"http\:\/\/(.*?)\"\starget=_blank\sclass=l/){
  120.             push(@sites,"http://$1");
  121.         }

  122.         if($urlre=~/\/search\?.*?q=.*?start=$nextpageno\&amp\;sa=N/){
  123.             $nextpage++;
  124.         }
  125.     }

  126.     my %seen=();
  127.     @sites = grep { !$seen{$_} ++ } @sites;
  128.     $siteno=0;

  129.     for my $nowsite (@sites)
  130.     {
  131.     $siteno++;

  132.     print "URL:$nowsite\n";
  133.     print WLOG $nowsite,"\n";
  134.     }
  135.     close WLOG;
  136.     if($nextpage==0)
  137.     {
  138.     cprin("\t\t\tThe End\n",13);
  139.     goto SPIDEREND;
  140.     }
  141. }

  142. SPIDEREND:
  143. $etime=time;
  144. $ttime=$etime-$stime;
  145. print stime($ttime);

  146. sub cprin
  147. {
  148.    ($str,$i)=@_;
  149.    cprint("\x03" . $i . "$str\n\x030");
  150. }

  151. sub stime
  152. {
  153.    my $stime=shift;
  154.    my $hour=int($stime/(60*60));
  155.    my $minute=int(($stime-($hour*60*60))/60);
  156.    my $second=$stime-$hour*60*60-$minute*60;
  157.    return ("$hour hours $minute mins $second secs");
  158. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-09-20 10:43 |只看该作者
厉害,学习一下

不过没做过坏事,还真不知道怎么用来做坏事,哈哈

论坛徽章:
0
3 [报告]
发表于 2010-09-20 10:59 |只看该作者
学习一下~

论坛徽章:
0
4 [报告]
发表于 2010-09-20 11:08 |只看该作者
学习ing

论坛徽章:
0
5 [报告]
发表于 2010-09-20 15:34 |只看该作者
怎么我刚才试了一下不行啊
就输出了一段时间信息。

356935 hours 28 mins 45 secs

论坛徽章:
0
6 [报告]
发表于 2010-09-20 17:50 |只看该作者
怎么我刚才试了一下不行啊
就输出了一段时间信息。

356935 hours 28 mins 45 secs
liht1981 发表于 2010-09-20 15:34



    详细错误信息有吗?  不可能  356935 hours  吧?

论坛徽章:
0
7 [报告]
发表于 2010-09-21 10:53 |只看该作者
回复 6# xti9er


   关键是没有报错,看来程序不是很robust。

论坛徽章:
0
8 [报告]
发表于 2010-09-21 19:49 |只看该作者
本帖最后由 xti9er 于 2010-09-21 19:51 编辑
回复  xti9er


   关键是没有报错,看来程序不是很robust。
liht1981 发表于 2010-09-21 10:53



     我这里完全 OK ,当然 旧代码 写 得稍微稚嫩了些 ,见谅 。

   

论坛徽章:
0
9 [报告]
发表于 2010-09-25 09:23 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP