自动获取天气与空气质量
发布时间:2020-12-16 00:04:22 所属栏目:大数据 来源:网络整理
导读:使用perl开发小工具,自动获取今天天气预报和空气质量,并定时发到自己邮箱中使用国家气象局api和抓取网页分析pm2.5的情况.使用perl解析json和正则处理进行处理.是使用linux下crontab 设置为晚上9点30执行这个脚本发到邮箱的内容位 天津 晴 -3℃ - 6℃ 2 pm2.5
使用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 -w use 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 = $1; 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::Errorn" ; $sender->Open( { to => "ruoshuiwyl@163.com",subject => "test",}) or die "Error: $Mail::Sender::Errorn" ; $sender->SendLineEnc( $result ); $sender->Close or die "Failed eto send message: $sender->{'error_msg'}n"; #system( "firefox -new-tab http://email.163.com/" ); exit; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |