本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
文章转载于公众号:奶忆口甜甜
作者: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 = '您的小可爱发来的人文关怀(○`(●●)´○)ノnn' text4 = '新的一天,我们一起努力ヾ(≧O≦)〃嗷~!n' content = text1,tomorrow,text2,text3,text4 return content
发送邮件函数
import yagmail ### 发送邮件 def send_email(contents,send_to = 'receiver_email@xx.com'): #登录邮箱,设置登录的账号,密码和 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 = 'receiver_email@xx.com') 的 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邮箱就不一样),需要根据自己的邮箱类型自行更改
参与评论
手机查看
返回顶部