python实现微信定时每天和女友发送消息

yipeiwu_com5年前Python基础

但凡有些事情重复时,我就在想怎么可以用程序来自动化。这里想分享如何每天给女友定时微信发送”晚安“,如果只是晚安,就略显单调,于是爬取金山词霸每日一句,英文和翻译,借此设定定时器进行发送。

准备:

pip install wxpy
pip install requests

实现代码:

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
import random
bot = Bot()
# linux执行登陆请调用下面的这句
#bot = Bot(console_qr=2,cache_path="botoo.pkl")
def get_news():
 
 """获取金山词霸每日一句,英文和翻译"""
 url = "http://open.iciba.com/dsapi/"
 r = requests.get(url)
 content = r.json()['content']
 note = r.json()['note']
 return content, note
 
def send_news():
 try:
 contents = get_news()
 # 你朋友的微信名称,不是备注,也不是微信帐号。
 my_friend = bot.friends().search('fairy')[0]
 my_friend.send(contents[0])
 my_friend.send(contents[1])
 my_friend.send(u"晚安")
 # 每86400秒(1天),发送1次
 t = Timer(86400, send_news)
 # 为了防止时间太固定,于是决定对其加上随机数
 ran_int = random.randint(0,100)
 t = Timer(86400+ran_int,send_news)
 
 t.start()
 except:
 
 # 你的微信名称,不是微信帐号。
 my_friend = bot.friends().search('威风大侠')[0]
 my_friend.send(u"今天消息发送失败了")
 
if __name__ == "__main__":
 send_news()

效果截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3使用matplotlib绘制条形图

python3使用matplotlib绘制条形图

本文实例为大家分享了python3使用matplotlib绘制条形图的具体代码,供大家参考,具体内容如下 绘制竖状条形图 代码 from matplotlib import pyp...

python实现12306火车票查询器

python实现12306火车票查询器

12306火车票购票软件大家都用过,怎么用Python写一个命令行的火车票查看器,要求在命令行敲一行命令来获得你想要的火车票信息,下面通过本文学习吧。 Python火车票查询器 接口...

Python Django实现layui风格+django分页功能的例子

Python Django实现layui风格+django分页功能的例子

第一步:首先定义一个视图函数,用于提供数据,实现每页显示数据个数,返回每页请求数据 from django.shortcuts import render from django.c...

python数据归一化及三种方法详解

python数据归一化及三种方法详解

数据标准化(归一化)处理是数据挖掘的一项基础工作,不同评价指标往往具有不同的量纲和量纲单位,这样的情况会影响到数据分析的结果,为了消除指标之间的量纲影响,需要进行数据标准化处理,以解决数...

Python流程控制 if else实现解析

Python流程控制 if else实现解析

一、流程控制 假如把程序比做走路,那我们到现在为止,一直走的都是直路,还没遇到过分岔口。当遇到分岔口时,你得判断哪条岔路是你要走的路,如果我们想让程序也能处理这样的判断,该怎么办?很简...