前端之家收集整理的这篇文章主要介绍了
Perl 按地区统计访问次数,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/usr/local/bin/perl
use LWP::Simple qw(get);
use Encode;
use Encode::CN;
use JSON;
use URI::Escape;
open (LOG,"<","129_2");
while (<LOG>){
$ip = $_;
print "hostip is $_\n";
$host = "http://ip.taobao.com/service/getIpInfo.PHP?ip=$_";
$content = get($host);
$content =~ s/\\u([0-9a-fA-F]{4})/pack("U",hex($1))/eg;
$content=encode(gbk,$content);
if ($content =~ /(.*?)"region":(.*?),"region_id"(.*?)"city":(.*?),(.*?)/){
$a = $2;
$b = $4;
$a =~ s/"//g ;
$b =~ s/"//g ;
if ($b ){$city = $b;}else{
$city = $a;};
$city =~ s/"//g ;
print "\$city is $city\n";
}
open (B,">>x.log");
print B ("$city $ip\n");
print "$content\n"};
open (LOG,"x.log");
while (<LOG>) {
chomp;
$var= (split / /,$_)[0];
$log{$var}++;
}
while(my($city,$times) = each %log) {
print "$city $times\n";
open (C,">>y.log");
print B ("$city $times\n");
}