自动获取天气与空气质量

自动获取天气与空气质量,第1张

概述使用perl开发小工具,自动获取今天天气预报和空气质量,并定时发到自己邮箱中使用国家气象局api和抓取网页分析pm2.5的情况.使用perl解析json和正则处理进行处理.是使用linux下crontab 设置为晚上9点30执行这个脚本发到邮箱的内容位 天津 晴 -3℃ - 6℃ 2 pm2.5 77 良滨州 晴 -6℃ - 7℃ -2 pm2.5 70 良 抓取网页使用lwp获取的 发 使用perl开发小工具,自动获取今天天气预报和空气质量,并定时发到自己邮箱中使用国家气象局API和抓取网页分析pm2.5的情况.使用perl解析Json和正则处理进行处理.是使用linux下crontab 设置为晚上9点30执行这个脚本发到邮箱的内容位
天津 晴 -3℃ - 6℃  2 pm2.5 77  良滨州 晴 -6℃ - 7℃  -2 pm2.5 70  良

抓取网页使用lwp获取的
发邮件是使用Mail::Sender完成


#!/usr/bin/perl -wuse 5.016;use Data::Dumper;use Encode;use LWP::UserAgent;use Mail::Sender;use JsON;use strict;use utf8;binmode( STDIN,":utf8" );binmode( STDOUT,":utf8" );binmode( STDERR,":utf8" );# 获取滨州,天津的天气预报和空气质量,发送给自己邮件my %cityIDs = (        "binzhou" => "101121101","tianjin" => "101030100",);my $brower = LWP::UserAgent->new;$brower->agent( "Mozilla/5.0 (X11; linux x86_64; rv:25.0) Gecko/20100101 firefox/25.0" );$brower->timeout( 10 );my $result;foreach my $city ( keys %cityIDs ){        my $cityID = $cityIDs{$city};        my $weather = "http://www.weather.com.cn/data/cityinfo/$cityID.HTML";        my $rweather = "http://www.weather.com.cn/data/sk/$cityID.HTML";        my $pm = "http://www.cnpm25.cn/City/$city.HTML";        my @urls = ( $weather,$rweather,$pm );        foreach my $url ( @urls ) {                my $req = $brower->get( $url );                unless( $req->is_success ) {                        say STDERR "无法获取网页";                        next;                }                if ( $url =~ m/weather/ ){                        my $Json = decode_Json( $req->content );                        my $weatherinfo = $Json->{'weatherinfo'};                        if ( defined( $weatherinfo->{'temp'} ) ){                                #print "  $weatherinfo->{'temp'} ";                                $result .= "  $weatherinfo->{'temp'}";                        } else {                                #print "$weatherinfo->{'city'} $weatherinfo->{'weather'} $weatherinfo->{'temp1'} - $weatherinfo->{'temp2'}" ;                                $result .= "$weatherinfo->{'city'} $weatherinfo->{'weather'} $weatherinfo->{'temp1'} - $weatherinfo->{'temp2'}" ;                        }                } else {                        if( $req->content =~ m/jin_value = "(\d+)"/ ){                                my $jin_value = ;                                my $jibIE = "优";                                if( $jin_value > 50 && $jin_value <= 100 ){                                        $jibIE = "良";                                } elsif ( $jin_value > 100 && $jin_value <= 150 ){                                        $jibIE = "轻度污染";                                } elsif ( $jin_value > 150 && $jin_value <= 200 ){                                        $jibIE = "中度污染";                                } elsif ( $jin_value > 200 && $jin_value <= 300 ){                                        $jibIE = "重度污染";                                } elsif ( $jin_value > 300 ){                                        $jibIE = "严重污染";                                }                                #say "pm2.5 $jin_value  $jibIE";                                $result .= " pm2.5 $jin_value  $jibIE";                        }                }        }        $result .= "\n";}#邮件my $sender = new Mail::Sender{        smtp => 'smtp.163.com',from => '******@163.com',charset => 'utf8',auth => 'LOGIN',authID => '********',//用户名        authpwd => '******',//密码        on_errors => 'dIE',TLS_allowed=> 0,} or dIE "Can not create the Mail::Sender object: $Mail::Sender::Error\n" ;$sender->Open( {                to => "ruoshuiwyl\@163.com",subject => "test",}) or dIE "Error: $Mail::Sender::Error\n" ;$sender->SendlineEnc( $result );$sender->Close or dIE "Failed eto send message: $sender->{'error_msg'}\n";#system( "firefox -new-tab http://email.163.com/" );exit;
总结

以上是内存溢出为你收集整理的自动获取天气与空气质量全部内容,希望文章能够帮你解决自动获取天气与空气质量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1278732.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存