微信小脚本之python调用wxpy
点击这里:wxpy官方文档链接
1. 安装wxpy
使用pip3安装:
pip3 install wxpy
python3中调用:
from wxpy import *
2. 模拟微信登陆和登出
Bot()跳出二维码–>扫码登陆;
当抛KeyError: 'pass_ticket'
异常时,可能是因为微信安全问题:不给登陆网页版微信
bot = Bot()
bot.logout()
登出微信
3. 批量处理好友和群聊等
获取所有好友:
all_friends = bot.friends()
print(all_friends)
所有群聊(只能是已存与通讯录的): bot.groups()
所有公众号: bot.mps()
3. 向好友发送消息
给自己发消息 bot.self.send('Hello World!')
给文件传输助手发消息 bot.file_helper.send('Hello World!')
找到好友(按备注搜索)
David = bot.friends().search('David')[0]
向该好友发送信息/图片
David.send('Hello!')
David.send_image('test.jpg')
4. 自动处理消息
注意: embed()函数加在某位,可以让脚本始终保持运行,除非报错
msg含有两个属性
- text: 消息内容
- type: 消息格式
脚本1:对所有消息一律回复(包括推送)
@bot.register()
def print_others(msg):
# 这里的msg是:文字则text格式,图片则picture格式
print(msg) # 打印对方发来的消息
msg.reply('您好我有事不在') # 自动回复
脚本2:回复指定好友消息
# David是friends().search()返回的对象
@bot.register(David)
def print_others(msg):
......
# 此时只回复Daivd发来的消息
脚本3:自动接收好友请求
@bot.register(msg_types=FRIENDS)
def auto_accept_friends(msg):
# 接受好友请求
new_friend = msg.card.accept()
# 向新的好友发送消息
new_friend.send('哈哈,我自动接受了你的好友请求')
脚本4:一个自动计算器
3*(5-2)
自动回复:9
@bot.register()
def print_others(msg):
if msg.type == 'Text':
msg.reply(str(eval(str(msg.text)))
embed()
脚本5:手写识别+计算器=小猿搜题简化版
待完成。。。
使用tesseract库