文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Perl用Post方法从新浪查天气

Perl用Post方法从新浪查天气

时间:2009-08-14  来源:dow

用Post方法从新浪查天气

#! /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/&nbsp;/ /g;
    print "-" x 50, "\n";
    print $_, " ";

    for my $line (1..13) {
        $_ = <$io>;
        chomp;
        1 while s/<[^<>]+>//g;
        s/\s//g;
        s/&nbsp;//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;
}

相关阅读 更多 +
排行榜 更多 +
打螺丝高手

打螺丝高手

模拟经营 下载
解救火柴人计划安卓版

解救火柴人计划安卓版

体育竞技 下载
鸡生化精英安卓版

鸡生化精英安卓版

飞行射击 下载