#! /usr/bin/perl -w
use strict;
use LWP;
use Encode;
use IO::String;
use Getopt::Long;
binmode( STDOUT, ":utf8" );
binmode( STDIN, ":utf8" );
binmode( STDERR, ":utf8" );
( my $usage = <<EOF ) =~ s/^\s+//g;
Usage: $0 [-h | --help] [city_name]
EOF
my $help = '';
GetOptions( help => \$help,
h => \$help
);
die $usage if $help;
my $city = shift || "广州";
$city = encode("gbk", decode("utf8", $city) );
my $c = &do_POST( "http://php.weather.sina.com.cn/search.php",
[ type => "text", city => $city ],
)
or die "Can not get weather infomation\n";
$c =~ s/gb2312/utf8/;
$c = decode("gbk", $c);
use utf8;
$/ = "\r\n";
my $io = IO::String->new($c);
while (<$io>) {
next unless /今日|明日|后天/;
chomp;
1 while s/<[^<>]+>//g;
s/\s//g;
s/ / /g;
print "-" x 50, "\n";
print $_, " ";
for my $line (1..13) {
$_ = <$io>;
chomp;
1 while s/<[^<>]+>//g;
s/\s//g;
s/ //g;
s/\s//g;
print $_, "\n" if length;
}
print "\n";
}
sub do_POST {
# Parameters:
# the URL,
# an arrayref or hashref for the key/value pairs,
# and then, optionally, any header lines: (key,value, key,value)
my $browser = LWP::UserAgent->new;
my $resp = $browser->post(@_);
return undef unless $resp->is_success;
return ($resp->content, $resp->status_line, $resp->is_success, $resp)
if wantarray;
return $resp->content;
}
|