pywechat是一个基于pywinauto实现的Windows系统下PC微信自动化(pure uiautomation)的项目(不涉及逆向Hook操作),可以用来收发消息和数据获取。 注意,本项目开发的初衷为使用UI自动化和分享UI自动化技术,忌勿将该工具用于违反微信使用条例的操作!
- 微信版本:3.9.12.x,4.1.6.x
- 操作系统:🪟7 🪟10 🪟11
- python版本:3.10+(支持TypeHint)
- 支持语言:简体中文,English,繁體中文
pywechat只支持旧版本微信(大部分方法仍然稳定可用),现在主要维护与更新支持4.1系列自动化的pyweixin pywechat使用说明
目前,可以使用的Skill的Agent主流平台:
具体使用方法可见QuickStart.md
使用示例:
实际效果:
pyweixin内所有方法需要先导入模块下的类然后调用内部方法☸︎
from pyweixin import xx(class)
xx(class).yy(method)Tools:关于PC微信的一些工具,微信路径,运行状态,以及内部一些UI相关的判别方法。Navigator:打开微信内部一切可以打开的界面。
AutoReply:自动回复操作。(一种简单的实现方式,如果对此感兴趣可以使用pyweixin内部的一些方法自行尝试)Call: 给某个好友打视频或语音电话。Contacts: 获取通讯录内各分区(联系人,企业微信联系人,公众号,服务号)好友的名称与详情。Files: 文件发送,聊天文件从本地导出等。FriendSettings: 针对某个好友的一些相关设置。Messages: 消息发送,聊天记录获取,聊天会话导出等。Moments:针对微信朋友圈的一些方法,包括朋友圈内容获取,发布朋友圈。Monitor:打开聊天窗口进行监听消息。
SystemSettings:该模块中提供了一些修改windows系统设置的方法(在自动化过程中)。
内部的一些函数主要用来二次开发,大部分传入的参数是main_window,pywinauto实例化的对象(使用Navigator.open_weixin打开)
At:在群聊中At指定的一些好友At_all:在群聊中At所有人auto_reply_to_friend_decorator:自动回复好友装饰器get_new_message_num:获取新消息总数,微信按钮上的红色数字scan_for_newMessages:会话列表遍历一遍有新消息提示的对象,返回好友名称与数量open_red_packet: 点击打开好友发送的红包language_detector:微信当前使用语言检测(不能禁用WeChatAppex.exe(涉及到公众号,微信内置浏览器,视频号等功能),原理是查询WeChatAppex.exe命令行参数)
所有自动化操作只需两行代码即可实现,即:
from pyweixin import xxx
xxx.yy'''
微信版本>=4.1.9,且配置过虚拟驱动,并且安装sounddevice与soundfile库,具体可见
https://mrcrab.blog.csdn.net/article/details/160481307?fromshare=blogdetail&sharetype=blogdetail&sharerId=160481307&sharerefer=PC&sharesource=weixin_73953650&sharefrom=from_link
''''
from pyweixin import Messages
Messages.send_audios_to_friend(friend='小号测试',audios=[r"E:\Desktop\录音.wav",r"E:\Desktop\音乐.mp3",r"E:\Desktop\音乐.ogg"])from pyweixin import Tools
print(Tools.about_weixin())from concurrent.futures import ThreadPoolExecutor
from pyweixin import Navigator,Monitor
#先打开所有好友的独立窗口
dialog_windows=[]
friends=['Hello,Mr Crab','Pywechat测试群']
durations=['1min']*len(friends)
for friend in friends:
dialog_window=Navigator.open_seperate_dialog_window(friend=friend,window_minimize=True,close_weixin=True)
dialog_windows.append(dialog_window)
with ThreadPoolExecutor() as pool:
results=pool.map(lambda args: Monitor.listen_on_chat(*args),list(zip(dialog_windows,durations)))
for friend,result in zip(friends,results):
print(friend,result)
#返回值 {'新消息总数':x,'文本数量':x,'文件数量':x,'图片数量':x,'视频数量':x,'链接数量':x,'文本内容':x,'消息发送人':x}from pyweixin import Monitor
#maxPages设置为0不滚动,只在顶部可见部分监听,全屏+maxPages为0+需要监听对象置顶=稳定安全
result=Monitor.listen_on_newMessages(duration='30s',maxPages=0)
print(result)from pyweixin import AutoReply
def reply_func(friend,newMessage):
return f'我在自动回复:{friend},这是对方发送的新消息:{newMessage}'
#maxPages设置为0不滚动,只在顶部可见部分自动回复,全屏+maxPages为0+需要回复对象置顶=稳定安全
responded_detail=AutoReply.auto_reply_messages(callback=reply_func,duration='1min',maxPages=0)
print(responded_detail)from pyweixin import Moments
#获取并导出今日前10个好友发布的朋友圈的具体内容
posts=Moments.dump_recent_posts(recent='Today',save_detail=True,number=10)
for dic in posts:
print(dic)from pyweixin import Moments
Moments.post_moments(texts='''发布朋友圈测试[旺柴]''',medias=[r"E:\Desktop\test0.png",r"E:\Desktop\test1.png"])from pyweixin import Moments
Moments.dump_friend_posts(friend='xxx',number=3,save_detail=True,target_folder=r"E:\Desktop\好友朋友圈内容导出")from pyweixin import Moments
def comment_func(content):
if 'xxx' in content:
return 'yyy'
return 'zzz'
Moments.like_friend_posts(friend='xxx',number=20,callback=comment_func)#注意,该方法不稳定!
from pyweixin import Collections
Collections.collect_offAcc_articles(name='新华社',number=10)
urls=Collections.cardLink_to_url(number=10)
for url,text in urls.items():
print(f'{text}\n{url}')from pyweixin import Collections
Collections.save_notes(number=1)from pyweixin import Navigator,GlobalConfig
GlobalConfig.load_delay=2.5
GlobalConfig.is_maximize=True
GlobalConfig.close_weixin=False
Navigator.search_channels(search_content='微信4.0')
Navigator.search_miniprogram(name='问卷星')
Navigator.search_official_account(name='微信')👎👎请勿将pywechat用于任何非法商业活动,因此造成的一切后果由使用者自行承担!
















