Python脚本:爬取天气数据并发邮件给心爱的Ta

时间:2021-09-07 10:44:06

第一部分:爬取天气数据

# 在函数调用 get_weather(url = 'https://www.tianqi.com/foshan') 的 url中更改城市,foshan为佛山市
 1 import requests
2 from lxml import etree
3
4 ### 爬取www.tianqi.com的今日和明日数据
5 def get_weather(url = 'https://www.tianqi.com/hangzhou/'):
6 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'}
7 html = requests.get(url,headers = headers)
8 bs = etree.HTML(html.text)
9 # 今天天气相关数据:日期,星期几,天气,最低气温,最高气温
10 today_date = bs.xpath('//ul[@class = "week"]/li[1]/b/text()')[0]
11 today_week = bs.xpath('//ul[@class = "week"]/li[1]/span/text()')[0]
12 today_weather = bs.xpath('//ul[@class = "txt txt2"]/li[1]/text()')[0]
13 today_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/b/text()')[0]
14 today_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/span/text()')[0]
15 # 明天天气相关数据,维度和上述一致
16 tomorrow_date = bs.xpath('//ul[@class = "week"]/li[2]/b/text()')[0]
17 tomorrow_week = bs.xpath('//ul[@class = "week"]/li[2]/span/text()')[0]
18 tomorrow_weather = bs.xpath('//ul[@class = "txt txt2"]/li[2]/text()')[0]
19 tomorrow_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/b/text()')[0]
20 tomorrow_high = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/span/text()')[0]
21 # 信息组合
22 tomorrow = ('明天是%s,%s,%s,%s-%s度,温差%d度')% \
23 (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low)))
24 #计算今明两天温度差异,这里用的是最高温度
25 temperature_distance = int(tomorrow_high) - int(today_high)
26 if temperature_distance > 0:
27 text2 = '明日升温%d' % temperature_distance
28 if temperature_distance < 0:
29 text2 = '明日降温%d' % temperature_distance
30 else:
31 text2 = '明日气温变化不明显'
32 #计算两个日期天数差
33 from dateutil import rrule
34 from datetime import datetime
35 import time
36
37 firstDay = datetime(2019,2,11)
38 endDay = datetime.now()
39 days = rrule.rrule(freq = rrule.DAILY,dtstart=firstDay,until=endDay)
40 text3 = '\n今天是我们在一起的第%d天\n' % days.count()
41
42 #设置输出内容和格式
43 text1 = '您的小可爱发来的人文关怀(○`(●●)´○)ノ\n\n'
44 text4 = '新的一天,我们一起努力ヾ(≧O≦)〃嗷~!\n'
45 content = text1,tomorrow,text2,text3,text4
46 return content

第二部分:发送邮件

# 在函数调用 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邮箱就不一样),需要根据自己的邮箱类型自行更改
 1 import yagmail
2
3 ### 发送邮件
4 def send_email(contents,send_to = 'receiver_email@xx.com'):
5 #登录邮箱,设置登录的账号,密码和 port等信息(发送方的)
6 yag = yagmail.SMTP(user = 'xxxxxxxxxxxxxxxxxxxxxxxx@qq.com',password = 'zqpokkghxmepgfcd',
7 host = 'smtp.qq.com',port = '465')
8 #登录完即可一键发送,设置发送给谁,和邮件主题,邮件内容
9 yag.send(to = send_to,
10 subject = 'I love U',
11 contents = contents)
12 print('发送成功!~')

第三部分:函数调用

contents = get_weather(url = 'https://www.tianqi.com/foshan/')
send_email(contents, send_to='xxxxxxxxxxxxxxxxxxxxxxx@qq.com')

本来的想法是做成一个每天定时自动发送天气邮件的脚本,但麻烦的点在于设备需要保持联网,所以就做了一次性的脚本。取走请留言。