第一部分:爬取天气数据

# 在函数调用 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')

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

最新文章

  1. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
  2. CentOS 6 中安装Node.js 4.0 版本或以上
  3. Android EditText控件即设置最小高度又运行高度随内容增加而变化
  4. 滑雪 why WA
  5. Ext Js【Hello World】 ——4.1 beta 1
  6. xcode简介
  7. iOS开发 创建单例模式
  8. 加载gif动态图的三种方式
  9. eclipse安装Gradle
  10. OPENGL画图类库
  11. java小提示:标示符常见命名规则、常用ASCII
  12. TDD 实践过程
  13. 百万程序员的苦恼-选择VB.NET还是C#
  14. 高效算法——G - 贪心
  15. linux loadavg详解(top cpu load)
  16. 贪心-poj-3040-Allowance
  17. MyEclipse10+Jdk1.7+OSGI+MySql实现CRUD数据库
  18. FCKeditor文字编辑器
  19. 对于python这门课程的一些想法、计划、期望
  20. ready

热门文章

  1. 由一次PasswordBox密码绑定引发的疑问 ---&gt; WPF中的附加属性的定义,以及使用。
  2. 【题解】hdu 3586 Information Disturbing 二分 树形dp
  3. 【.NET 与树莓派】TM1638 模块的按键扫描
  4. 『心善渊』Selenium3.0基础 — 9、使用Seleniun中的By类定位元素
  5. C#获取字符串字符的位数(区分中文和英文长度)
  6. Binding(四):数据校验
  7. Pandas高级教程之:plot画图详解
  8. Result Maps collection already contains value for cn.itcast.ssm.mapper.CompetesMapperCustom.baseMap
  9. linux学习之路第九天(任务调度)
  10. WPF教程十四:了解元素的渲染OnRender()如何使用