您的位置:首页 > 其它

Perl结合飞信发送免费的天气预报信息

2011-07-22 16:42 579 查看
每天在公司上班,打开QQ的同时,就顺便看了一下今天的天气情况。当时就在想,可不可以用perl来解析这个html页面,抽取其中必要的elements,然后透过第三方的飞信将该消息发送给自己,不就可以了么。说干就干,就写了这么一段代码,算法不是特别好,参考了某位网友的处理方法,但又有区别。

获取天气预报的页面地址,我用了这个address

http://qq.ip138.com/weather/zhejiang/HangZhou.htm

完整的code如下:(调试了一天,妈妈的)

#!/usr/bin/perl -w

use strict;
#utf8一定要加,否则出现乱码

use utf8;
use LWP::Simple;
use 5.010;

my $url = shift || "http://qq.ip138.com/weather/zhejiang/HangZhou.htm";
my $content = get $url;
my @url = split /\n/,$content;
my $path = "/root/lib";
my $fetion = "/root/lib/fetion";

&get_weather(\@url);
system(qq{LD_LIBRARY_PATH=$path $fetion --mobile=12345678901 --pwd='123456' --to=123456 --exit-on-verifycode=1 --file-utf8=/root/weather.txt --msg-type=1});

sub get_weather($) {
my ( $weather ) = @_;
my ( $count,$i ) = ( 0,0 );

while ( $i < scalar(@$weather) ) {

next unless @$weather[$i++] =~ /日期/;
$i += 1;

open my $file,'>>','/root/weather.txt' or die "$!\n";
if ( -s '/root/weather.txt' > 0 ) {
system("cat /dev/null >/root/weather.txt");
}

while ( $count < 1 ) {
@$weather[$i++] =~ /(?:.*)\>(?<name1>.*?)\<\/td\>/;
$count ++;
say $file "$+{name1}\t";
}

say $file "\n";
$i += 9;
$count = 0;
while ( $count < 1 ) {
@$weather[$i++] =~ /.*\>(?<name2>.*)\<\/td\>/;
$count ++;
say $file "$+{name2}\t";
}

say $file "\n";
$i += 9;
$count = 0;
while ( $count < 1 ) {
@$weather[$i++] =~ /(?:.*)\>(?<name3>.*?)\<\/td\>/;
$count ++;
say $file "$+{name3}\t";
}

close $file;
last;

}
}

此支perl程序,仅仅抽取了天气预报详情页面的这几个值:

1) 日期

2) 实际天气情况

3)当天的气温

其他的就没有弄了,情况类似。

附:linux下配置飞信的方法

下载机器人支持库

http://www.it-adv.net/fetion/linuxso_20101113.rar

注:我的系统是64位的,但是如果下载了64位的版本,ms有问题,32位的就OK

另外,linux用户,请不要把支持库中的 lib* 复制到 /usr/lib 下,因为发行版本不同,可能会覆盖您机器中的核心库,导致严重系统问题。您可以把库解压到主程序的相同目录,然后以 LD_LIBRARY_PATH=. ./fetion 来运行)

详细介绍见这个页面

http://bbs.it-adv.net/viewthread.php?tid=1081&extra=page%3D1
本文出自 “BSDerの-专注于开源领域” 博客,请务必保留此出处http://hellosa.blog.51cto.com/2698675/620743
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: