对于网速慢,被墙的朋友,你们有福了,我现在把我经常获取可以的代理服务器地址的代码发出来
?
?
?
- ########################################## ?
- ?
- #获取有效http代理服务器列表程序 ?
- ?
- #作者:yifangyou ?
- ?
- #创建时间:2011-03-27?17:14:00 ?
- ?
- #本程序实现 ?
- ?
- #1.从代理网站获取代理服务器列表 ?
- ?
- #2.逐个检测代理服务器是否可用 ?
- ?
- ########################################## ?
- ?
- package?yifangyou; ?
- use?LWP::Simple; ?
- use?LWP::UserAgent; ?
- use?strict;? ?
- use?warnings; ?
- use?LWP::Simple?qw(?$ua?get?); ?
- #代理服务器ip和端口存放的地方 ?
- ?
- my?@proxys=(); ?
- #已经检测完的代理服务器ip和端口存放的地方 ?
- ?
- my?@checkedProxys=(); ?
- #已经检测完的代理服务器ip和端口存放的地方,我用的百度首页的图标 ?
- ?
-
my?$stdUrl="http://www.baidu.com/img/baidu_sylogo1.gif";? ?
- #代理服务器列表页面的url,可以加多个页面 ?
- ?
-
my?@urls=("http://www.5uproxy.net/http_fast.html");#,"http://www.5uproxy.net/http_anonymous.html","http://www.5uproxy.net/http_non_anonymous.html"); ?
- ?
- $ua->timeout(3);?#?3秒超时 ?
- ?
-
$ua->agent("Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?5.1)");?#?模拟xp?ie7浏览器 ?
- ?
- #获取待验证的代理服务器列表 ?
- ?
- foreach?my?$url(@urls){ ?
- ????getProxyAddr($url); ?
- } ?
- #验证代理服务器列表 ?
- ?
- foreach?my?$proxyAddr(@proxys){ ?
- ????checkProxyAddr($stdUrl,$proxyAddr); ?
- } ?
- #输出所有结果 ?
- ?
-
print?join(",",@checkedProxys)."n"; ?
- ?
- #获取待验证的代理服务器列表 ?
- ?
- sub?getProxyAddr ?
- { ?
- ??????my?$url=shift; ?
- ????my?$document?=?get($url); ?
-
????return?print?"cannot?get?$urln"?unless?defined?$document; ?
-
????my?@lines=split("n",$document); ?
- ????my?$i=0; ?
- ????my?@matches; ?
-
????for($i=0;$i<@lines;$i++){ ?
- ????????#获取ip ?
- ?
- ????????if(@matches=$lines[$i]=~/(d{1,3}.d{1,3})/i)?{ ?
- ????????????my?$ip=$matches[0]; ?
- ????????????#获取端口 ?
- ?
- ????????if(@matches=$lines[$i+1]=~/<td.*>(d{1,6})</td>/i)?{ ?
- ???????????my?$port=$matches[0]; ?
-
???????????push(@proxys,"$ip:$port"); ?
- ????????}? ?
- ??????} ?
- ????} ?
- } ?
- ?
- #验证代理服务器列表 ?
- ?
- sub?checkProxyAddr{ ?
- ????my?$url=shift; ?
- ????my?$proxyAddr=shift; ?
- ?? ?
-
??$ua->proxy(['http'],?"http://$proxyAddr");?#?设http代理服务器 ?
- ?
- ??my?@headcontent=head($url);?#取head比较快 ?
- ?
- ??if(scalar(@headcontent)==0){ ?
-
#?????print?"Could?not?get?head?from?$proxyAddrn"; ?
- ?
-
??????return?-1;????????? ?
-
??}else{ ?
-
??????????print?"$proxyAddrn"; ?
-
??????????push(@checkedProxys,"$proxyAddr"); ?
-
??????????return?0; ?
- ??}???? ?
- #????my?$ua?=?new?LWP::UserAgent; ?
- ?
- #?$ua->timeout(3);?#?3秒超时 ?
- ?
-
#?$ua->proxy(['http'],?"http://$proxyAddr");?#?设http代理服务器 ?
- ?
-
#?$ua->agent("Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?5.1)");?#?模拟xp?ie7浏览器 ?
- ?
- #?my?$req?=?new?HTTP::Request->new(GET?=>?$url); ?
- ?
- #?my?$res?=?$ua->request($req); ?
- ?
- #?if?($res->is_success){ ?
- ?
-
#?????????print?"$proxyAddrn"; ?
- ?
-
#?????????push(@checkedProxys,"$proxyAddr"); ?
- ?
-
#?????????return?0; ?
- ?
-
#?}else?{ ?
- ?
-
#?????print?"Could?not?get?head?from?$proxyAddrn"; ?
- ?
-
#?????return?-1; ?
- ?
- #????} ?
- ?
- }?