大家都知道www.ip138.com可以查手机号码归属地,但总感觉一个一个去查询特别麻烦,今天尝试着用perl写了个小脚本.获取号码归属地和SIM卡的类型.
- #!/usr/bin/perl -w
- use warnings;
- use strict;
- use LWP::UserAgent;
- use HTML::TreeBuilder;
- my $ip_url = 'http://www.ip138.com:8080/search.asp?action=mobile&mobile=';
- open DATA,"$ARGV[0]";
- sub get_html {
- my $browser = LWP::UserAgent->new();
- $browser->agent('Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1)');
- my $resp = $browser->get(@_);
- # return ($resp->content,$resp->status_line,$resp->is_success,$resp) if wantarray;
- return unless $resp->is_success;
- return $resp->content
- }
- while (<DATA>){
- chomp;
- print "$_\t";
- my $link = $ip_url.$_;
- my $cont = get_html("$link","Accept-language"=>"zh-cn",'Accept-Charset'=>'gb2312',);
- #print "$cont\n";
- my $table = $1 if $cont =~ m/<TABLE width=349 border="1" align="center" cellpadding="4" bordercolor=#3366cc style="border-collapse: collapse">(.*?)<\/TABLE>/s;
- $table =~ s/ \;/ /g;
- # print "$table\n";
- my $tree = HTML::TreeBuilder->new();
- $tree->strict_comment(1);
- $tree->parse($table);
- $tree->eof();
- my $tr = ($tree->find_by_tag_name('tr'))[2];
- my $tr1 = ($tree->find_by_tag_name('tr'))[3];
- my @kids = $tr->content_list( );
- my @class = $tr1->content_list();
- print $kids[1]->as_text( ), "\t";
- print $class[1]->as_text(),"\n";
- $tree->delete;
- sleep(1);
- }
结果如下:
转载于:https://blog.51cto.com/donghui/662768