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

yipeiwu_com6年前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设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

Ubuntu下Python2与Python3的共存问题

Linux系统一般自带Python,有时候又自己下载了Python,因此有可能Python2和Python3同时存在。那么当我们在Terminal键入python的时候,会调出哪个Pyt...

Python实现的彩票机选器实例

Python实现的彩票机选器实例

本文实例讲述了Python实现彩票机选器的方法。分享给大家供大家参考。具体实现方法如下: # -*- coding: utf8 -*- from Tkinter import * i...

Python的Twisted框架中使用Deferred对象来管理回调函数

Python的Twisted框架中使用Deferred对象来管理回调函数

首先抛出我们在讨论使用回调编程时的一些观点: 激活errback是非常重要的。由于errback的功能与except块相同,因此用户需要确保它们的存在。他们并不是可选项,而是必选项...

python 连接各类主流数据库的实例代码

本篇博文主要介绍Python连接各种数据库的方法及简单使用 包括关系数据库:sqlite,mysql,mssql 非关系数据库:MongoDB,Redis 代码写的比较清楚,直接上代码...

python连接mysql并提交mysql事务示例

复制代码 代码如下:# -*- coding: utf-8 -*-import sysimport MySQLdbreload(sys)sys.setdefaultencoding('u...