Python定时发送消息的脚本:每天跟你女朋友说晚安

yipeiwu_com5年前Python基础

首先 你要有个女朋友

效果:

需要安装几个包

  • pip install wxpy
  • pip install wechat_sender
  • pip install requests

代码如下:

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
from wechat_sender import Sender
import time,requests
bot = Bot(console_qr=2,cache_path='botoo.pk1') # 把consol_qr=2去掉,二维码是当做图片弹出来,否则则是以像素的方式打印出来,后面的参数是热登录,
def get_news():
  # 这里是把今日糍粑每日一句中拿过来的信息发送给你朋友
  url = "http://open.iciba.com/dsapi/"
  r = requests.get(url)
  contents = r.json()['content']
  translation = r.json()['translation']
  return contents,translation
def send_news():
  try:
    ufriend = bot.friends().search(u'Mr-Lee')[0] # 朋友微信昵称(不是备注,不是微信账号)
    ufriend.send(get_news()[0])
    ufriend.send(get_news()[1][5:])
    ufriend.send(u'晚安')
    t = Timer(86400,send_news) # 86400是间隔时间(一天)
    t.start()
  except:
    ufriend = bot.friends().search("L")[0] # 你的微信名称,不是微信号
    ufriend.send(u'消息发送失败')
if __name__ == '__main__':
  send_news()

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

python 实现在Excel末尾增加新行

实例如下所: import os import xlrd import xlwt from xlutils.copy import copy def excelwrite(L=No...

使用Python的OpenCV模块识别滑动验证码的缺口(推荐)

使用Python的OpenCV模块识别滑动验证码的缺口(推荐)

最近终于找到一个好的方法,使用Python的OpenCV模块识别滑动验证码的缺口,可以将滑动验证码中的缺口识别出来了。   测试使用如下两张图片:   target....

django admin.py 外键,反向查询的实例

如下所示: class OrderAdmin(admin.ModelAdmin): list_display = ( '_nick_name', 'time_order'...

Python下singleton模式的实现方法

很多开发人员在刚开始学Python 时,都考虑过像 c++ 那样来实现 singleton 模式,但后来会发现 c++ 是 c++,Python 是 Python,不能简单的进行模仿。...

Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】

本文深入分析了Python面向对象程序设计OOP。分享给大家供大家参考,具体如下: 下面是一个关于OOP的实例,模块文件为person.py # File person.py(sta...