Chinaunix首页 | 论坛 | 博客
  • 博客访问: 634553
  • 博文数量: 184
  • 博客积分: 10057
  • 博客等级: 上将
  • 技术积分: 2505
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-31 16:34
文章分类

全部博文(184)

文章存档

2010年(5)

2009年(104)

2008年(75)

我的朋友

分类:

2009-08-14 23:07:36

用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( "",
                  [ 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;
}

阅读(1244) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~