利用perl脚本查询手机号码的地区信息:
use LWP;
use HTML::TreeBuilder;
my $phone = $ARGV[0];
if(!defined($phone))
{
print "号码不能为空,请输入电话号码.";
exit(-1);
}
my $geo,$type,$zone,$code;
my $found_geo = 0,$found_type = 0,$found_zone = 0,$found_code = 0;
my $ua = LWP::UserAgent->new();
my $reponse = $ua->get("http://www.ip138.com:8080/search.asp?mobile=$phone&action=mobile");
my $content = $reponse->content;
#print $content;
my $tree = HTML::TreeBuilder->new;
$content =~ s/ //g;
$tree->parse($content);
$tree->eof();
#$tree->dump();
foreach my $row ( $tree->find_by_tag_name("tr") ) {
foreach my $cell ( $row->content_list ) {
if($cell->as_text =~ /卡号归属地/)
{
$found_geo = 1;
next;
}
elsif($cell->as_text =~ /卡类型/)
{
$found_type = 1;
next;
}
elsif($cell->as_text =~ /区 号/)
{
$found_zone = 1;
next;
}
elsif($cell->as_text =~ /邮 编/)
{
$found_code = 1;
next;
}
if($found_geo)
{
$geo = $cell->as_text;
$found_geo = 0;
next;
}
if($found_type)
{
$type = $cell->as_text;
$found_type = 0;
next;
}
if($found_zone)
{
$zone = $cell->as_text;
$found_zone = 0;
next;
}
if($found_code)
{
$code = $cell->as_text;
$found_code = 0;
last;
}
}
}
print "手机号码: $phone/n";
print "卡号归属地: $geo/n";
print "卡类型: $type/n";
print "区 号:$zone/n";
print "邮 编:$code/n";
$tree->delete();