最近工作开始忙起来了,也不看天气预报了.直到今天下雨把我淋透了..看来,天气预报还是很重要的.所以写了个小脚本获取 www.ip138.com 接口上的天气预报.通过139邮箱发送到手机端.代码如下:

  1. #!/usr/bin/perl 
  2. use warnings; 
  3. use strict; 
  4. use LWP::UserAgent; 
  5. use HTML::TreeBuilder; 
  6. use Net::SMTP; 
  7. use Net::SMTP_auth; 
  8. use MIME::Base64; 
  9. #my $wday= (localtime(time()))[6]; 
  10. my $i = '2'
  11. my $j = '3'
  12. my $weather_url = "http://qq.ip138.com/weather/beijing/HaiDian.htm"
  13. my $cont = get_html($weather_url,"Accept-language" => "zh-cn"'Accept-Charset' => 'gb2312'); 
  14. my $table = $1 if $cont =~ m/<p align="center"><table width="700" borderColorDark="#ffffff" borderColorLight="#008000" bor 
  15. der="1" cellspacing="0" cellpadding="1" align="center">(.*?)<\/p>/s; 
  16. my $tree = HTML::TreeBuilder->new(); 
  17.    $tree->parse($table); 
  18.    $tree->eof(); 
  19. #   print $tree->dump; 
  20. my @th = $tree->find_by_tag_name("th"); 
  21. my @tr = $tree->find_by_tag_name("td"); 
  22. my $tomorrow = $th[$i]->as_text()."  ". $tr[$i]->as_text()." ".$tr[$i+8]->as_text()." ".$tr[$i+16]->as_text(); 
  23. my $after_day = $th[$j]->as_text()."  ". $tr[$j]->as_text()." ".$tr[$j+8]->as_text()." ".$tr[$j+16]->as_text(); 
  24.    $tree->delete(); 
  25.    send_mail("$tomorrow","$after_day"); 
  26. =pod 
  27. foreach my $row($tree->find_by_tag_name("tr")){ 
  28.         foreach my $cell($row->content_list){ 
  29.                 print $cell->as_text."\t"
  30.  } 
  31. print "\n"
  32. =cut 
  33. sub get_html { 
  34.         my $url = shift; 
  35.         my $browser= LWP::UserAgent->new(); 
  36.            $browser->agent("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;SV1;)"); 
  37.         my $resp = $browser->get($url); 
  38.         return unless $resp->is_success; 
  39.         return  $resp->content; 
  40. sub send_mail { 
  41.         my $data1 = shift; 
  42.         my $data2 = shift; 
  43.         my $smtp_mail_host = 'smtp.sinanet.com'
  44.         my $mail_user_from = 'donghui@leju.sina.com.cn'
  45.         my $mail_user_to = 'yisildeai@139.com'
  46.         my $mail_user_pass = "P@ssW0rd"
  47.         my $mail_hello = 'mail.sinanet.com'
  48.         my $smtp = Net::SMTP->new( 
  49.                 Host => "$smtp_mail_host"
  50.                 Hello => "$mail_hello"
  51.                 timeout => 40, 
  52.                 Debug => 1,)or die "can not connect mail server!\n"
  53.         $smtp->auth("$mail_user_from","$mail_user_pass")or die "auth faild!\n"
  54.         $smtp->mail("$mail_user_from"); 
  55.         $smtp->to("$mail_user_to"); 
  56.         $smtp->data(); 
  57.         $smtp->datasend("From: donghui <donghui\@leju.sina.com.cn>\n"); 
  58.         $smtp->datasend("To: <donghui\@.leju.sina.com.cn>\n"); 
  59.         $smtp->datasend("Subject: <天气预报>\n"); 
  60.         $smtp->datasend("Content-Type: text/plain;charset=\"gb2312\"\n"); 
  61.         $smtp->datasend("Content-Transfer-Encoding: base64\n"); 
  62.         $smtp->datasend(encode_base64("Dear: \n")); 
  63.         $smtp->datasend(encode_base64(" $data1\n $data2\n")); 
  64.      #  $smtp->datasend(encode_base64(" The day after torrow: $data2\n")); 
  65.         $smtp->datasend(encode_base64("------------\n")); 
  66.         $smtp->datasend(encode_base64("Evil\'  ^_^")); 
  67.         $smtp->datasend("\n\n"); 
  68.         $smtp->dataend(); 
  69.         $smtp->quit();   

效果如下:

perl获取天气预报信息结合139邮箱推送到手机端_perl

当然了,你的手机也会定时收到.不过前提你的SIM卡类型为China_mobile.

再定义一个crond任务 每天晚上8点定时发送.

00 20 */1 * * /usr/bin/perl /usr/local/share/info/get_weather.pl

#service crond restart

#chkconfig crond on

大功告成..起码每个月省了两块钱的天气预报短信.呵呵....