为了在第一时间知道这几天的天气状况(家里有baby,所以需要每天的天气状态),我参考网上和自己的一些想法,写了下面的脚本。
#!/usr/bin/perl
use strict;
use warnings;
use URI;
use Web::Scraper;
use URI::Escape;
use LWP::Simple;
my $url = "http://www.tianqiyubao.org/weather_shanghai";
my $sender_user="1391845xxxx";
my $sender_pwd="yyyyxx";
my $reciver_user="1391845xxxx";
my $manager="1391845xxxx";
my $manager_pwd="yyyyxx";
my $log_control = 1;
my $worker = scraper {
process "code", "weathers[]" => "TEXT";
};
#print "Starting...\n";
my $result = $worker->scrape( URI->new($url) );
for my $weather ( @{ $result->{weathers} } ) {
`/home/badboy/perl/fetion --mobile=$sender_user --pwd=$sender_pwd --to=$reciver_user --msg-utf8="$weather"`||&save_log("Cant send weather report:$!");
}
sub save_log{
if($log_control) {
my $fh;
open($fh, '>> sms.log') or die "can't open log: $!";
print $fh localtime(time)."\t".$_[0]."\n";
close $fh;
`/home/badboy/perl/fetion --mobile=$manager --pwd=$manager_pwd --to=$manager --msg-utf8="不能发送天气预报,请检查!"`
}
}
操作系统:
badboy@badboy-desktop:~/perl/sendsms$ more /etc/issue
Ubuntu 10.04.2 LTS \n \l
本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/573484,如需转载请自行联系原作者