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

相关文章

教你学会使用Python正则表达式

教你学会使用Python正则表达式

今天写爬虫偶然想到了初学正则表达式时候,看过一篇文章非常不错。检索一下还真的找到了。 re模块 re.search 经常用match = re.search(pat, str)的形式...

python 实现turtle画图并导出图片格式的文件

python 实现turtle画图并导出图片格式的文件

如下所示: from turtle import* import turtle setup(800,700,300,50) penup() seth(90) fd(100) seth...

使用Python进行QQ批量登录的实例代码

具体代码如下所示: #coding=utf-8 __author__ = 'Eagle' import os import time import win32gui import w...

基于python的汉字转GBK码实现代码

基于python的汉字转GBK码实现代码

如图,“广”的编码为%B9%E3,暂且把%B9称为节编码,%E3为字符编码(第二编码)。 思路: 从GBK编码页面收集汉字 http://ff.163.com/newflyff/gbk-...

Python 二叉树的层序建立与三种遍历实现详解

Python 二叉树的层序建立与三种遍历实现详解

前言 二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有。。。。(此处省略好多字)。。。。等的优良特点。 之前在刷LeetCode的时候把有关树的题目全部跳过了,(O...