博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python实现个人定制天气预报
阅读量:5888 次
发布时间:2019-06-19

本文共 4150 字,大约阅读时间需要 13 分钟。

# -*- encoding:utf-8 -*-import requests,json,smtplibimport sysreload(sys)sys.setdefaultencoding('utf-8')#city =['WS0E9D8WN298','W7JZGDR3YYTF']city ='W7JZGDR3YYTF'def getweather(city):    url ='https://api.seniverse.com/v3/weather/daily.json?key=nuvsytsinmz49ufn&location='+ city +'&language=zh-Hans&unit=c&start=0&days=5'    f =requests.get(url)    weather_dict=json.loads(f.text,encoding='utf-8')    date=weather_dict[u'results'][0][u'daily'][0][u'date']    text_day=weather_dict[u'results'][0][u'daily'][0][u'text_day']    text_night=weather_dict[u'results'][0][u'daily'][0][u'text_night']    high_temp =weather_dict[u'results'][0][u'daily'][0][u'high']    low_temp=weather_dict[u'results'][0][u'daily'][0][u'low']    wind_direction=weather_dict[u'results'][0][u'daily'][0][u'wind_direction']    wind_scale=weather_dict[u'results'][0][u'daily'][0][u'wind_scale']        if text_day in ['雷阵雨','大雨','中雨','小雨','阵雨','暴雨'] and high_temp>=30:        weather_info=unicode('温馨提示:xxxx,记得带雨伞哦,白天气温比较高,记得多喝水')+'\n'+unicode('城市:三亚')+'\n'+unicode('日期:') + date +'\n'+ unicode('白天天气:')+text_day +'\n'+ unicode('晚上天气:')+text_night +'\n'+ unicode('最高温度:')+ high_temp +'\n'+unicode('最低温度:')+low_temp+ '\n'+unicode('风向:')+wind_direction+'\n'+unicode('风力:')+wind_scale+'\n'        elif text_day in ['雷阵雨','大雨','中雨','小雨','阵雨','暴雨'] and 20<=high_temp<30:        weather_info=unicode('温馨提示:xxxx,记得带雨伞哦')+'\n'+unicode('城市:三亚')+'\n'+unicode('日期:') + date +'\n'+ unicode('白天天气:')+text_day +'\n'+ unicode('晚上天气:')+text_night +'\n'+ unicode('最高温度:')+ high_temp +'\n'+unicode('最低温度:')+low_temp+ '\n'+unicode('风向:')+wind_direction+'\n'+unicode('风力:')+wind_scale+'\n'        elif text_day in ['雷阵雨','大雨','中雨','小雨','阵雨','暴雨'] and high_temp<20:        weather_info=unicode('温馨提示:xxxx,记得带雨伞,天气有点凉,记得要加件衣服')+'\n'+unicode('城市:三亚')+'\n'+unicode('日期:') + date +'\n'+ unicode('白天天气:')+text_day +'\n'+ unicode('晚上天气:')+text_night +'\n'+ unicode('最高温度:')+ high_temp +'\n'+unicode('最低温度:')+low_temp+ '\n'+unicode('风向:')+wind_direction+'\n'+unicode('风力:')+wind_scale+'\n'    elif text_day not in ['雷阵雨','大雨','中雨','小雨','阵雨','暴雨'] and high_temp>=30:        weather_info=unicode('温馨提示:xxxx,白天气温比较高,记得多喝水')+'\n'+unicode('城市:三亚')+'\n'+unicode('日期:') + date +'\n'+ unicode('白天天气:')+text_day +'\n'+ unicode('晚上天气:')+text_night +'\n'+ unicode('最高温度:')+ high_temp +'\n'+unicode('最低温度:')+low_temp+ '\n'+unicode('风向:')+wind_direction+'\n'+unicode('风力:')+wind_scale+'\n'        elif text_day not in ['雷阵雨','大雨','中雨','小雨','阵雨','暴雨'] and 20<=high_temp<30:        weather_info=unicode('温馨提示:xxxxx,今天天气不错哦')+'\n'+unicode('城市:三亚')+'\n'+unicode('日期:') + date +'\n'+ unicode('白天天气:')+text_day +'\n'+ unicode('晚上天气:')+text_night +'\n'+ unicode('最高温度:')+ high_temp +'\n'+unicode('最低温度:')+low_temp+ '\n'+unicode('风向:')+wind_direction+'\n'+unicode('风力:')+wind_scale+'\n'        elif text_day not in ['雷阵雨','大雨','中雨','小雨','阵雨','暴雨'] and high_temp<20:        weather_info=unicode('温馨提示:xxxxx,今天天气不错哦,白天有点凉,记得加件衣服')+'\n'+unicode('城市:三亚')+'\n'+unicode('日期:') + date +'\n'+ unicode('白天天气:')+text_day +'\n'+ unicode('晚上天气:')+text_night +'\n'+ unicode('最高温度:')+ high_temp +'\n'+unicode('最低温度:')+low_temp+ '\n'+unicode('风向:')+wind_direction+'\n'+unicode('风力:')+wind_scale+'\n'    return weather_infoSMTPServer='smtp.phone580.com'fromaddr ='xxxxxxxxxx'toaddr =('xxxxxx','xxxxxxx')MsgHead =['From:xxxxxxxxx','To:xxxxxxxxx,xxxxxxxxxxx','Subject:个人定制天气预报']MsgBody =[getweather(city).encode('utf-8')]Msg ='\r\n\r\n'.join(['\r\n'.join(MsgHead),'\r\n'.join(MsgBody)])s =smtplib.SMTP(SMTPServer)s.set_debuglevel(1)s.login('xxxxxxxxxxxx','xxxxxxxxxxx')s.sendmail(fromaddr,toaddr,Msg)s.quit()
注意编码问题解決方法: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)所以解決方法有3種一種是全都轉byte string 一種是全都轉unicode string 第三種是更改設定預設解碼器為utf-8app engine上我不知道怎麼更改設定預設解碼器 所以只說前2種1.全都轉byte stringself.response.out.write("你好"+self.request.get("argu").encode('utf-8'))2.全都轉unicode stringself.response.out.write(u"你好"+self.request.get("argu"))P.S.資料庫存入和讀取以及self.request拿到的參數預設就都是unicode string,若是要把byte string轉unicode string可以這樣轉unicode(unicodestring,"utf-8")

转载于:https://www.cnblogs.com/yang1208/p/7363586.html

你可能感兴趣的文章
打开远程桌面命令
查看>>
LAMP架构(nginx安装,默认虚拟主机,用户认证,域名重定向,nginx配置文件详解)...
查看>>
Spring Boot多数据源配置与使用
查看>>
Spring Data + Thymeleaf 3 + Bootstrap 4 实现分页器
查看>>
对Spring IOC的理解
查看>>
javascript中childNodes.length兼容性问题
查看>>
SQL语句的一些基础
查看>>
Eclispe Java代码注释模板
查看>>
设置 SSH 通过密钥登录
查看>>
leadtools
查看>>
仿百度搜索框自动完成提示功能
查看>>
PHP的学习--Traits新特性
查看>>
GnuPG如何安全地分发私钥(5)分发我的私钥(+签名)
查看>>
高性能golang后端处理网络模块包
查看>>
android面试题
查看>>
test
查看>>
郭宇:Airpub - 纯前端博客引擎实践
查看>>
Progress Image View
查看>>
GHGLUtils
查看>>
开源 免费 java CMS - FreeCMS1.5-数据对象-job
查看>>