要想完成这个需求,需要用到python中的两个库,itchat库和itchat-uos,2017年后,新注册的微信很难登录网页版,itchat-uos利用统信UOS的网页版微信,可以帮助我们绕开网页微信的登录限制。本次使用的版本是 python3.7 版本

一、安装该模块并导入

pip install itchat itchat-uos

import itchat from itchat.content import *

二、使用

itchat的文档可参考https://itchat.readthedocs.io/zh/latest/

1.登录与退出微信

def logout(): itchat.logout() print("退出成功") def login(): print("登录成功") #hotReload=True 一定时间内重新开启也可以不用重新扫码 itchat.auto_login(exitCallback=logout,hotReload=True) itchat.run()

2.保存并发送给该好友

itchat.content中包含所有的消息类型参数有:

TEXT -- 文本消息 MAP -- 位置文本 CARD -- 名片 NOTE -- 通知 PICTURE -- 图片/表情 SHARING -- 分享 RECORDING -- 语音 ATTACHMENT -- 附件 VIDEO -- 小视频

@itchat.msg_register([TEXT, NOTE, SHARING,PICTURE, RECORDING, ATTACHMENT, VIDEO]) def reply(msg): if msg.type == "Text": pass # msg.user.send(msg.text) elif msg.type in ["Recording","Picture","Attachment","Video"]: msg.download(msg.fileName) itchat.send('@%s@%s' % ( 'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName']) else: print(msg.type)

3.效果如图

怎么把python代码发给别人(如何用python保存语音)(1)

,