Python 微信聊天记录监控工具 - 实时记录聊天内容
导入必要的库
import itchat from itchat.content import *
定义一个字典,用来保存聊天记录
chat_records = {}
处理文本消息
@itchat.msg_register(TEXT) def text_reply(msg): # 获取发送者和接收者的用户名 sender = msg['FromUserName'] receiver = msg['ToUserName'] # 如果发送者不是自己,将聊天记录保存到字典中 if sender != itchat.get_friends()[0]['UserName']: # 如果聊天记录中没有该发送者的记录,创建一个空列表 if sender not in chat_records: chat_records[sender] = [] # 将聊天记录添加到列表中 chat_records[sender].append(msg['Text'])
保存聊天记录到文件
def save_records(): with open('chat_records.txt', 'w') as f: for sender, records in chat_records.items(): f.write('Chat records with ' + sender + ': ') f.write('-' * 30 + ' ') f.write(' '.join(records)) f.write('
')
登录微信并启动监控
def main(): itchat.auto_login(hotReload=True) itchat.run()
# 监控结束后保存聊天记录到文件
save_records()
if name == 'main': main()
原文地址: https://www.cveoy.top/t/topic/mz0E 著作权归作者所有。请勿转载和采集!