1. 程式人生 > 其它 >給女朋友每日天氣預報

給女朋友每日天氣預報

urllib庫呼叫免費的天氣API介面,獲取所在城市的天氣資訊(可以通過抓包工具抓取對應城市的citycode)。

大家應該知道Bing的背景圖比較漂亮,每日會更新,獲取圖片的API也是公開的,可以呼叫。圖片下載到本地 將天氣資訊和圖片利用python的smtp模組郵件傳送給女朋友。

還可以將python程式上傳到伺服器上,加入一條定時任務,每天定時執行程式。

# coding=utf-8
import urllib3
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import parseaddr, formataddr import smtplib import xml.etree.ElementTree as ET import time import json,ast def _format_addr(s): name, addr = parseaddr(s) print (name,addr) headers = Header(name, '
utf-8').encode() result =formataddr((headers, addr)) return result # 獲取bing的每日圖片,修改api中的引數n,可以獲取多張圖片 # 從bing的api返回的xml中提取圖片的url,然後下載到本地,並命名為yyyymmdd.jpg def get_bing_pic(): http = urllib3.PoolManager() r = http.request("GET","http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1") webpage
= r.data root = ET.fromstring(webpage) # 獲取圖片連結 url = 'http://cn.bing.com' + root.find('image').find('url').text # 圖片描述 pic_desc_ = root.find('image').find('copyright').text # 圖片儲存到本地的位置 pic_file_name_ = "d://" + time.strftime('%Y%m%d', time.localtime(time.time()))+".jpg" urllib.request.urlretrieve(url, pic_file_name_ ) #urllib2.urlretrieve(url, pic_file_name_ + url[-4:]) return pic_file_name_ , pic_desc_ # 呼叫免費的天氣API介面,city_code預設為廣州的code:101280101 中山的code:101281701,其他城市code請使用抓包工具 def get_city_weather(city_code='101281701'): http = urllib3.PoolManager() r = http.request("GET","http://www.weather.com.cn/data/cityinfo/"+city_code+".html") print ((r.data.decode())) weather_json = ast.literal_eval(r.data.decode()) #weather_json= json.load(r.data.decode()) #response = urllib2.urlopen("http://www.weather.com.cn/data/cityinfo/"+city_code+".html") #weather_json = json.loads(r.data.read()) city_ = weather_json['weatherinfo']['city'] min_temp_ = weather_json['weatherinfo']['temp1'] max_temp_ = weather_json['weatherinfo']['temp2'] weather_ = weather_json['weatherinfo']['weather'] return city_, min_temp_, max_temp_, weather_ # 發件人地址和密碼 from_addr = 'mylove<[email protected]>' from_addr1 = '[email protected]' password = 'xxxKRCHDKGHIFONZHGO' # SMTP伺服器地址: smtp_server = 'smtp.163.com' # 收件人地址: to_addr = 'mylove1<[email protected]>' # 抄送給自己: cc_addr = 'mylove<[email protected]>' city, min_temp, max_temp, weather = get_city_weather() # 帶路徑的檔名 pic_file_name, pic_desc = get_bing_pic() # 檔名 pic_name = pic_file_name.split("/")[-1] # 這裡是郵件正文內容,''是為了將附件的圖片插入郵件正文中,直接顯示 content = u"Hello %s今天天氣%s,最低溫度%s,最高溫度%s .<br> %s <br> Have a nice day." % (city, weather, min_temp, max_temp, pic_desc) print (content) msg = MIMEMultipart() msg['From'] = _format_addr(from_addr) msg['To'] = _format_addr(to_addr) msg['Cc'] = _format_addr(cc_addr) #msg['From'] = from_addr #msg['To'] = to_addr #msg['Cc'] = cc_addr msg['Subject'] = Header(u'每日天氣播報', 'utf-8').encode() msg.attach(MIMEText(content, 'html', 'utf-8')) # 新增附件就是加上一個MIMEBase,從本地讀取一個圖片 with open(pic_file_name, 'rb') as f: # 設定附件的MIME和檔名,這裡是jpg型別 mime = MIMEBase('image', 'jpg', filename=pic_name) # 加上必要的頭資訊 mime.add_header('Content-Disposition', 'attachment', filename=pic_name) mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') # 把附件的內容讀進來 mime.set_payload(f.read()) # 用Base64編碼 encoders.encode_base64(mime) # 新增到MIMEMultipart msg.attach(mime) server = smtplib.SMTP(smtp_server, 25) # SMTP協議預設埠是25 # server.starttls() #SSL加密的SMTP,注意修改埠號 server.set_debuglevel(0) # 列印和郵件伺服器互動的詳細資訊 server.login(from_addr1, password) server.sendmail(from_addr, [to_addr, cc_addr], msg.as_string()) server.quit()