Python实现个人定制天气预报

# -*- encoding:utf-8 -*-
import requests,json,smtplib
import sys
reload(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_info
SMTPServer='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-8

app engine上我不知道怎麼更改設定預設解碼器 所以只說前2種

1.全都轉byte string
self.response.out.write("你好"+self.request.get("argu").encode('utf-8'))

2.全都轉unicode string
self.response.out.write(u"你好"+self.request.get("argu"))
P.S.資料庫存入和讀取以及self.request拿到的參數預設就都是unicode string,若是要把byte string轉unicode string可以這樣轉unicode(unicodestring,"utf-8")
posted @ 2017-08-15 10:05  绿叶1208  阅读(1569)  评论(0编辑  收藏  举报