本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
文章轉載于公眾號:奶憶口甜甜
作者:YangHonghui
腳本功能
在天氣網上獲取今日和明日天氣資料
計算兩日溫差
生成郵件發送至TA的郵箱,
具體實作
爬取天氣資料函式
import requests from lxml import etree ### 爬取www.tianqi.com的今日和明日資料 def get_weather(url = 'https://www.tianqi.com/hangzhou/'): headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} html = requests.get(url,headers = headers) bs = etree.HTML(html.text) # 今天天氣相關資料:日期,星期幾,天氣,最低氣溫,最高氣溫 today_date = bs.xpath('//ul[@class = "week"]/li[1]/b/text()')[0] today_week = bs.xpath('//ul[@class = "week"]/li[1]/span/text()')[0] today_weather = bs.xpath('//ul[@class = "txt txt2"]/li[1]/text()')[0] today_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/b/text()')[0] today_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/span/text()')[0] # 明天天氣相關資料,維度和上述一致 tomorrow_date = bs.xpath('//ul[@class = "week"]/li[2]/b/text()')[0] tomorrow_week = bs.xpath('//ul[@class = "week"]/li[2]/span/text()')[0] tomorrow_weather = bs.xpath('//ul[@class = "txt txt2"]/li[2]/text()')[0] tomorrow_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/b/text()')[0] tomorrow_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/span/text()')[0] # 資訊組合 tomorrow = ('明天是%s,%s,%s,%s-%s度,溫差%d度')% \ (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low))) #計算今明兩天溫度差異,這里用的是最高溫度 temperature_distance = int(tomorrow_high) - int(today_high) if temperature_distance > 0: text2 = '明日升溫%d' % temperature_distance if temperature_distance < 0: text2 = '明日降溫%d' % temperature_distance else: text2 = '明日氣溫變化不明顯' #計算兩個日期天數差 from dateutil import rrule from datetime import datetime import time firstDay = datetime(2019,2,11) endDay = datetime.now() days = rrule.rrule(freq = rrule.DAILY,dtstart=firstDay,until=endDay) text3 = '\n今天是我們在一起的第%d天\n' % days.count() #設定輸出內容和格式 text1 = '您的小可愛發來的人文關懷(○`(●●)′○)?\n\n' text4 = '新的一天,我們一起努力ヾ(≧O≦)〃嗷~!\n' content = text1,tomorrow,text2,text3,text4 return content
發送郵件函式
import yagmail ### 發送郵件 def send_email(contents,send_to = '[email protected]'): #登錄郵箱,設定登錄的賬號,密碼和 port等資訊(發送方的) yag = yagmail.SMTP(user = '你的郵箱',password = '你的授權碼', host = 'smtp.qq.com',port = '465') #登錄完即可一鍵發送,設定發送給誰,和郵件主題,郵件內容 yag.send(to = send_to, subject = 'I love U', contents = contents) print('發送成功!~')
函式呼叫
contents = get_weather(url = 'https://www.tianqi.com/foshan/') send_email(contents, send_to='對方的郵箱')
代碼說明
# 在函式呼叫 parse(url = 'https://www.tianqi.com/foshan') 的 url中更改城市,foshan為佛山市 # Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 # 是UserAgent,這是用于瀏覽器識別的,可以看出你的系統版本,瀏覽器,瀏覽器內核等 # 在函式呼叫 send_email(contents,send_to = '[email protected]') 的 send_to中更改接收方郵箱地址 # 在函式定義 send_email 的 user,password,host,port中更改發送方的郵箱賬號,密碼和 host和 port等資訊 # 其中,password 不能直接用郵箱密碼,而要改為授權碼,授權碼獲取方式(如 QQ郵箱:https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256%27) # 其中,host 和 post 不同的郵箱型別不一樣(如,網易郵箱和 QQ郵箱就不一樣),需要根據自己的郵箱型別自行更改
最終效果
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/113572.html
標籤:Python
