perl 公交车查询

时间:2023-12-12 11:51:44
<pre name="code" class="cpp">decode_json 必须是unicode形式的字符,Dump不支持显示unicode形式的中文 只能 \x{xxxx}
use URI::Escape;
use Encode;
use JSON qw/encode_json decode_json/;
my $str="杭州";
my $str=uri_escape("$str");
print "\$str is $str\n";
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my @header = (
'accept'=> "application/json",
'content-type'=> "application/json",
'apikey'=> "3074a7f9926a4bbb484aa8bb366e5b87",
'User-Agent' => "Mozilla/8.0"
);
my $url = "http://apis.baidu.com/xiaota/bus_lines/buses_lines?city=$str&bus=$ARGV[0]&direction=$ARGV[1]";
$request = HTTP::Request->new(GET=>"$url");
$request->header(@header);
$response = $ua->request($request);
$content= $response->decoded_content;
$content =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg;
$content = encode_utf8($content);
my $json_out = $content ;
print $json_out;
##解码json数据
my $json_out = decode_json($content);
print "111111111111\n";
use Data::Dumper; #my $json_out= Dumper($json_out->{data}->{stations});
#print $json_out;
#print "\n";
foreach (@{($json_out->{data}->{stations})}){print encode_utf8($_->{stateName})."\n";};
[root@dr-mysql01 ~]# perl x2.pl 307 1
$str is %E6%9D%AD%E5%B7%9E
{"code":1000,"msg":"success!","data":{"buses":[{"busId":0,"station":10,"state":0,"distance":246,"reporTime":1}],"stations":[{"station":1,"stateName":"黄龙体育中心"},{"station":2,"stateName":"教工路花园亭"},{"station":3,"stateName":"花园新村"},{"station":4,"stateName":"浙江工商大学"},{"station":5,"stateName":"保亭巷"},{"station":6,"stateName":"教工路塘河路口"},{"station":7,"stateName":"大关桥西"},{"station":8,"stateName":"长乐路"},{"station":9,"stateName":"二纺机总厂"},{"station":10,"stateName":"沈塘湾"},{"station":11,"stateName":"善贤社区"},{"station":12,"stateName":"善贤坝"},{"station":13,"stateName":"皋亭坝"},{"station":14,"stateName":"李王桥"},{"station":15,"stateName":"袁家村"},{"station":16,"stateName":"张家园"},{"station":17,"stateName":"杭玻"},{"station":18,"stateName":"焦化分厂"},{"station":19,"stateName":"沈家桥"},{"station":20,"stateName":"毛竹山"},{"station":21,"stateName":"薄板分厂"},{"station":22,"stateName":"刘文村"}]}}111111111111
黄龙体育中心
教工路花园亭
花园新村
浙江工商大学
保亭巷
教工路塘河路口
大关桥西
长乐路
二纺机总厂
沈塘湾
善贤社区
善贤坝
皋亭坝
李王桥
袁家村
张家园
杭玻
焦化分厂
沈家桥
毛竹山
薄板分厂
刘文村