今天同事有个小需求,有600多个手机号码,需要在http://ipseeker.cn/mobile.php上查询其归属地和卡的类型。
简单想了想,可以利用perl的http模块来做这件事,不过有点麻烦的是,该网页是使用post数据的,不知道perl能不能支持。上网找了找资料,幸好,居然找到了一块支持post数据的样本代码。
剩下来的就是体力活了。这个小需求,用perl不到1个小时就解决了,perl绝对的很好很强大!
贴一下perl的源代码,以备不时之需。
#!/usr/bin/perl -w
use strict;
use LWP 5.64;
my $total_suc = 0;
sub QueryMobile
{
my ($mobile) = @_;
my $result_str = $mobile;
my $browser = LWP::UserAgent->new;
$browser->timeout( 10);
my @ns_headers = (
'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Accept' => '*/*',
'Accept-Charset' => 'gzip, deflate',
'Accept-Language' => 'zh-cn'
);
my $response = $browser->post( "http://ipseeker.cn/mobile.php", ["mobile"=>$mobile,"Submit"=>"查询","job"=>"search"], @ns_headers );
if($response->is_success){
my $content = $response->content;
open FD, ">temp.html";
print FD $content;
close FD;
open FD, "temp.html";
foreach(<FD>)
{
chomp;
if (m/查询结果/i)
{
if (m/所属地区:([^<]*)<br>/i)
{
$result_str .= " $1";
}
if (m/卡 类 型:([^<]*)</i)
{
$result_str .= " $1" ;
}
$total_suc++;
close FD;
`del temp.html`;
return $result_str;
}
}
close FD;
`del temp.html`;
}
$result_str;
}
open TARGET, ">mobile_result.txt";
open READ_TARGET, "ret_mobile.txt" or die "can't open file:$!";
my @all_lines = <READ_TARGET>;
my $all_mobile = @all_lines;
my $start_time = time;
foreach (@all_lines)
{
chomp;
my $result = QueryMobile $_;
print TARGET $result . "/n";
print $result . "/n";
}
my $using_time = time - $start_time;
print "Query $all_mobile mobiles, success: $total_suc, using $using_time secs/n";
close READ_TARGET;
close TARGET;