python实现给微信指定好友定时发送消息

yipeiwu_com5年前Python基础

python有很多有趣的库,其中wxpy是连接微信的接口,具体可以查看官方文档。可以实现自动操作,wxpy 支持 Python 3.4-3.6,以及 2.7 版本。

一、安装

win10环境,直接在cmd中,输入

pip install wxpy

有时网络不稳定,可能出现错误,重新执行操作尝试一下。

二、简单介绍

# 导入模块
from wxpy import *
# 初始化机器人,扫码登陆
bot = Bot()
# 搜索名称含有 "游否" 的男性深圳好友
my_friend = bot.friends().search('游否', sex=MALE, city="深圳")[0]

三、详细代码

打开cmd,执行jupyter notebook,打开ipython环境,在打开的浏览器页面中,新建一个python3的ipynb文件。

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
 
bot = None
def get_news():
 #获取一个连接中的内容
 url = "http://open.iciba.com/dsapi/"
 r = requests.get(url)
 print(r.json())
 contents = r.json()['content']
 translation = r.json()['translation']
 return contents,translation
def login_wechat():
 global bot
 bot = Bot()
 # bot = Bot(console_qr=2,cache_path="botoo.pkl")#linux环境上使用
def send_news():
 if bot == None:
  login_wechat()
 try:
  my_friend = bot.friends().search(u'xxx')[0] #xxx表示微信昵称
  my_friend.send(get_news()[0])
  my_friend.send(get_news()[1][5:])
  my_friend.send(u"咦?我是自动人!!")
  t = Timer(360, send_news) #360是秒数
  t.start()
 except:
  print(u"失败!!")
if __name__ == "__main__":
 send_news()
 print(get_news()[0])

然后按ctrl+enter键执行。

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

相关文章

python/Matplotlib绘制复变函数图像教程

python/Matplotlib绘制复变函数图像教程

今天发现sympy依赖的库mpmath里也有很多数学函数,其中也有在复平面绘制二维图的函数cplot,具体例子如下 from mpmath import * def f1(z):...

Python学习笔记_数据排序方法

1. 原地排序:采用sort()方法,按照指定的顺序排列数据后用排序后的数据替换原来的数据(原来的顺序丢失),如: 复制代码 代码如下:>>> data1=[4,2,6...

Python定义函数功能与用法实例详解

本文实例讲述了Python定义函数功能与用法。分享给大家供大家参考,具体如下: 1.函数的意义 一般数学上的函数是,一个或者几个自变量,通过某种计算方式,得出一个因变量。 y = f(...

python实现颜色空间转换程序(Tkinter)

python实现颜色空间转换程序(Tkinter)

本文主要基于colorsys实现,样例是从hls转换到rgb,如果要换颜色空间很容易只需要修改一个函数,具体内容如下 用到了Scale和Canvas组件。 运行效果图: 代码如下:...

Flask实现跨域请求的处理方法

在Flask开发RESTful后端时,前端请求会遇到跨域的问题。下面是解决方法: 使用 flask-cors库可以很容易的解决 pip install flask-cors 两种方...