python给微信好友发消息_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键执行。

blank.gif

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 1969-12-31

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值