用Python写一个微信提醒备忘录 !做一个时间规划表!

最近工作比较繁杂,经常忘事,有时候记了备忘录结果却忘记看备忘录,但是微信是每天都会看的,于是就想到写 一个基于微信的提醒系统。总体思路是将待办事项记录到在线记事本,通过建立定时任务,每天早上爬取文档中记 录的待办事项,筛选出当日需要处理的事项,并通过server酱发送到自己微信。

1任何的记录与提取

1).任务记录

为了便于爬取,推荐使用网页版的在线记事本,现在这种工具很多,我选择“石墨文档”进行操作演示。记录内容的 格式可以根据自己的需求和爬虫自行确定,例如我在11月20日之前记录了一份以下样式的任务清单:

Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。

11月21日 9:30 吃饭

11月21日 12:30 睡觉

11月21日 14:00 打豆豆

11月22日 9:00 去南极旅游

11月23日 18:00 请豆豆吃火锅

2).模拟登录

使用爬虫来实现待办事项的提取,发现在线记事本是需要登录的:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557644955478″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python写一个微信提醒备忘录 !做一个时间规划表!》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

模拟登录的方法很多,这里我选择selenium模拟的方式进行登录操作,分别在“手机号或邮箱”和“密码”两栏输入自 己的登录名和密码,然后点击“立即登录”按钮完成登陆,部分核心代码如下:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557644955483″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python写一个微信提醒备忘录 !做一个时间规划表!》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

3).任务提取

登录后要指定跳转到记录自己待办事项的页面,然后使用selenium.webdriver对象的page_source属性可以很容易获取到页面的html,经过解析获取待办事项的数据信息。代码如下:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557644955487″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python写一个微信提醒备忘录 !做一个时间规划表!》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

当然,有时候我们会在记事本里记录很多需要办理的事项,执行日期也会比较分散,但是没必要每天对所有的待办事项进行提醒。对此,我们可以筛选出当天需要办理的事项进行提醒,这样效果无疑会更好一些。任务筛选的基本思路是获取系统当天日期,将它与各事项的待办日期逐一进行匹配,代码如下:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557644955490″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python写一个微信提醒备忘录 !做一个时间规划表!》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

2信息推送到微信

1).server酱

说到推送信息到微信,不得不介绍一个很方便的工具——server酱。说的简单一点,server酱就是一款能向目标微信账号发送信息的工具,她最大的有点就是功能简单、使用方便、轻量,使用server酱只需下面几个步骤:

  • 登入:用GitHub账号登入网站,获得自己的SCKEY。
  • 绑定:通过微信扫码关注完成绑定。
  • 发消息:往 http://sc.ftqq.com/SCKEY.send 发GET请求,就可以在微信里收到消息。

另外,server酱还提供了网页版的在线发送工具,可以进行体验和测试。值得一提的是,server酱的初始目的是通过手机监测服务器报警和日志,因此为避免程序出错,限制了每人每天发 送上限500条,相同内容5分钟内不能重复发送,不同内容一分钟只能发送30条。对于本系统来说,这个数据上限 绝对绰绰有余了。

2)信息推送

介绍完了server酱,就来试试她的效果吧。

首先我们要明确发送get请求的地址,即上面第三步中的URL,这个URL的关键取决于自己的SCKEY,之后要设置发 送信息的标题和内容,然后向目标URL发送请求即可完成信息的推送,顺利 的话马上就能在微信中收到server酱的信息提醒了。

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557644955496″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python写一个微信提醒备忘录 !做一个时间规划表!》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

3定时任务与检测

1)定时任务

现在我们需要设置一个定时任务,这里使用APScheduler来实现。APScheduler是基于Quartz的一个python定时任务框架,提供了若干种类的调度器,由于我们的定时任务进在当前进程中执行,所以选择BlockingScheduler调度器,然后通过add_job来动态增加待执行的任务。

关于APScheduler,其实并不需要了解太多,因为它的基本用法非常简单:

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1557644955500″ data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《用Python写一个微信提醒备忘录 !做一个时间规划表!》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

  • entrance为主功能函数,用于爬取在线记事本中的信息、筛选出当天需要办理的事项并通过server酱发送到 微信;
  • day_of_week表示定时任务在一周中的哪几天执行,0-6分别代表周一至周日,也可以指定某个值,比如‘0’表 示仅在周一执行;
  • hour、minute和second分别代表定时任务执行的时、分、秒,这几个参数既可以指定某个值也可以向day_of_week一样设置范围,甚至设置执行的间隔频率。

将程序放到服务中,通过start()方法启动定时任务调度器,这样在每天早上的7:30微信中就会收到当天待办事项 的有关提示。

2).运行监测

现在还有一个问题,就是要保证我们的程序始终处于正常运行的状态,上面说过,server酱通过向目标URL发送get请求的方式推送信息,而这个get请求是有返回值的,其格式为“{‘errno’: 0, ‘errmsg’: ‘success’, ‘dataset’: ‘done’}”,其中若errno为0则表示发送成功,否则会在errmsg中给出错误信息。基于此,我们可以依托logging模块记录程序每天的运行状态,以便及时发现问题。相关代码如下:

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>logging.basicConfig(filename=’log.log’, format=’%(asctime)s:%(message)s’, level=logging.INFO)
if response.json()[‘errno’] == 0:
logging.info(‘发送成功’)
else:
logging.warning(‘发送失败 %s’ %response.json()[‘errmsg’])
</pre>

(代码左右滑动)

最后,我们来演示一下系统的运行效果,以文章开头部分的记事本内容为例,在11月21日这天的早上微信中会收 到这样一条信息(下图),终于不用再担心遗忘什么重要工作了。

    原文作者:不谈风月_0eb8
    原文地址: https://www.jianshu.com/p/7e9837ea7fa4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞