用perl获取可用的代理服务器地址

对于网速慢,被墙的朋友,你们有福了,我现在把我经常获取可以的代理服务器地址的代码发出来

 

 

 

 
 
  1. ##########################################  
  2.  
  3. #获取有效http代理服务器列表程序  
  4.  
  5. #作者:yifangyou  
  6.  
  7. #创建时间:2011-03-27 17:14:00  
  8.  
  9. #本程序实现  
  10.  
  11. #1.从代理网站获取代理服务器列表  
  12.  
  13. #2.逐个检测代理服务器是否可用  
  14.  
  15. ##########################################  
  16.  
  17. package yifangyou;  
  18. use LWP::Simple;  
  19. use LWP::UserAgent;  
  20. use strict;   
  21. use warnings;  
  22. use LWP::Simple qw( $ua get );  
  23. #代理服务器ip和端口存放的地方  
  24.  
  25. my @proxys=();  
  26. #已经检测完的代理服务器ip和端口存放的地方  
  27.  
  28. my @checkedProxys=();  
  29. #已经检测完的代理服务器ip和端口存放的地方,我用的百度首页的图标  
  30.  
  31. my $stdUrl="http://www.baidu.com/img/baidu_sylogo1.gif";   
  32. #代理服务器列表页面的url,可以加多个页面  
  33.  
  34. my @urls=("http://www.5uproxy.net/http_fast.html");#,"http://www.5uproxy.net/http_anonymous.html","http://www.5uproxy.net/http_non_anonymous.html");  
  35.  
  36. $ua->timeout(3); # 3秒超时  
  37.  
  38. $ua->agent("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); # 模拟xp ie7浏览器  
  39.  
  40. #获取待验证的代理服务器列表  
  41.  
  42. foreach my $url(@urls){  
  43.     getProxyAddr($url);  
  44. }  
  45. #验证代理服务器列表  
  46.  
  47. foreach my $proxyAddr(@proxys){  
  48.     checkProxyAddr($stdUrl,$proxyAddr);  
  49. }  
  50. #输出所有结果  
  51.  
  52. print join(",",@checkedProxys)."\n";  
  53.  
  54. #获取待验证的代理服务器列表  
  55.  
  56. sub getProxyAddr  
  57. {  
  58.       my $url=shift;  
  59.     my $document = get($url);  
  60.     return print "cannot get $url\n" unless defined $document;  
  61.     my @lines=split("\n",$document);  
  62.     my $i=0;  
  63.     my @matches;  
  64.     for($i=0;$i<@lines;$i++){  
  65.         #获取ip  
  66.  
  67.         if(@matches=$lines[$i]=~/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/i) {  
  68.             my $ip=$matches[0];  
  69.             #获取端口  
  70.  
  71.         if(@matches=$lines[$i+1]=~/<td.*>(\d{1,6})<\/td>/i) {  
  72.            my $port=$matches[0];  
  73.            push(@proxys,"$ip:$port");  
  74.         }   
  75.       }  
  76.     }  
  77. }  
  78.  
  79. #验证代理服务器列表  
  80.  
  81. sub checkProxyAddr{  
  82.     my $url=shift;  
  83.     my $proxyAddr=shift;  
  84.     
  85.   $ua->proxy(['http'], "http://$proxyAddr"); # 设http代理服务器  
  86.  
  87.   my @headcontent=head($url); #取head比较快  
  88.  
  89.   if(scalar(@headcontent)==0){  
  90. #     print "Could not get head from $proxyAddr\n";  
  91.  
  92.       return -1;           
  93.   }else{  
  94.           print "$proxyAddr\n";  
  95.           push(@checkedProxys,"$proxyAddr");  
  96.           return 0;  
  97.   }      
  98. #    my $ua = new LWP::UserAgent;  
  99.  
  100. # $ua->timeout(3); # 3秒超时  
  101.  
  102. # $ua->proxy(['http'], "http://$proxyAddr"); # 设http代理服务器  
  103.  
  104. # $ua->agent("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); # 模拟xp ie7浏览器  
  105.  
  106. # my $req = new HTTP::Request->new(GET => $url);  
  107.  
  108. # my $res = $ua->request($req);  
  109.  
  110. # if ($res->is_success){  
  111.  
  112. #         print "$proxyAddr\n";  
  113.  
  114. #         push(@checkedProxys,"$proxyAddr");  
  115.  
  116. #         return 0;  
  117.  
  118. # }else {  
  119.  
  120. #     print "Could not get head from $proxyAddr\n";  
  121.  
  122. #     return -1;  
  123.  
  124. #    }  
  125.  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值