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

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

概述 第一部分:爬取天气数据#在函数调用 get_weather(url = 'https://www.tianqi.com/foshan') 的 url中更改城市,foshan为佛山市1importrequests2fromlxmlimportetree34###爬取www.tianqi.com的今日和明日数据5defget_weather(url='https://www.tianq

 

第一部分:爬取天气数据

# 在函数调用 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度')%            (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_distance28     if temperature_distance < 0:29         text2 = '明日降温%d' % temperature_distance30     else:31         text2 = '明日气温变化不明显'32     #计算两个日期天数差33     from dateutil import rrule34     from datetime import datetime35     import time36 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,text446     return content

 

第二部分:发送邮件

# 在函数调用 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邮箱就不一样),需要根据自己的邮箱类型自行更改
 1 import yagmail 2  3 ### 发送邮件 4 def send_email(contents,send_to = '[email protected]'): 5     #登录邮箱,设置登录的账号,密码和 port等信息(发送方的) 6     yag = yagmail.SMTP(user = '[email protected]',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='[email protected]')

 

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

 

总结

以上是内存溢出为你收集整理的Python脚本:爬取天气数据并发邮件给心爱的Ta全部内容,希望文章能够帮你解决Python脚本:爬取天气数据并发邮件给心爱的Ta所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1186438.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存