python如何查看微信消息撤回
作者:孙智斌 时间:2023-07-31 23:18:09
本文为大家分享了python查看微信消息撤回的具体代码,供大家参考,具体内容如下
1.安装itchat
itchat是一个开源的python微信库,支持发送消息、图片、视频、地图、名片、文件等,还可以实现自动回复等多种功能。
看到的一个文档还不错
1).先安一个python 下载地址
2).安装成功之后,进入pip3.exe所在的目录
安装itchat:pip3.exe install itchat 下图我的已经安装过了(注意:使用3以及以上版本执行)。
3.打开cmd拖入按回车就好了
代码实现
建一个wx.py文件,以下代码导入
# -*-encoding:utf-8-*-
import os
import re
import shutil
import time
import itchat
from itchat.content import *
# 说明:可以撤回的有文本文字、语音、视频、图片、位置、名片、分享、附件
# {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
msg_dict = {}
# 文件存储临时目录
rev_tmp_dir = "/RevDir/"
if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir)
# 表情有一个问题 | 接受信息和接受note的msg_id不一致 巧合解决方案
face_bug = None
# 将接收到的消息存放在字典中,当接收到新消息时对字典中超时的消息进行清理 | 不接受不具有撤回功能的信息
# [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO])
def handler_receive_msg(msg):
global face_bug
# 获取的是本地时间戳并格式化本地时间戳 e: 2017-04-21 21:30:08
msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 消息ID
msg_id = msg['MsgId']
# 消息时间
msg_time = msg['CreateTime']
# 消息发送人昵称 | 这里也可以使用RemarkName备注但是自己或者没有备注的人为None
msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"]
# 消息内容
msg_content = None
# 分享的链接
msg_share_url = None
if msg['Type'] == 'Text' \
or msg['Type'] == 'Friends':
msg_content = msg['Text']
elif msg['Type'] == 'Recording' \
or msg['Type'] == 'Attachment' \
or msg['Type'] == 'Video' \
or msg['Type'] == 'Picture':
msg_content = r"" + msg['FileName']
# 保存文件
msg['Text'](rev_tmp_dir + msg['FileName'])
elif msg['Type'] == 'Card':
msg_content = msg['RecommendInfo']['NickName'] + r" 的名片"
elif msg['Type'] == 'Map':
x, y, location = re.search(
"<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3)
if location is None:
msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()
else:
msg_content = r"" + location
elif msg['Type'] == 'Sharing':
msg_content = msg['Text']
msg_share_url = msg['Url']
face_bug = msg_content
# 更新字典
msg_dict.update(
{
msg_id: {
"msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
"msg_type": msg["Type"],
"msg_content": msg_content, "msg_share_url": msg_share_url
}
}
)
# 收到note通知类消息,判断是不是撤回并进行相应操作
@itchat.msg_register([NOTE])
def send_msg_helper(msg):
global face_bug
if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None:
# 获取消息的id
old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
old_msg = msg_dict.get(old_msg_id, {})
if len(old_msg_id) < 11:
itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
os.remove(rev_tmp_dir + face_bug)
else:
msg_body = "爱你蕾蕾 ⇣" + "\n" \
+ old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" \
+ old_msg.get('msg_time_rec') + "\n" \
+ "内容 ⇣" + "\n" \
+ r"" + old_msg.get('msg_content')
# 如果是分享存在链接
if old_msg['msg_type'] == "Sharing": msg_body += "\n就是这个链接➣ " + old_msg.get('msg_share_url')
# 将撤回消息发送到文件助手
itchat.send(msg_body, toUserName='filehelper')
# 有文件的话也要将文件发送回去
if old_msg["msg_type"] == "Picture" \
or old_msg["msg_type"] == "Recording" \
or old_msg["msg_type"] == "Video" \
or old_msg["msg_type"] == "Attachment":
file = '@fil@%s' % (rev_tmp_dir + old_msg['msg_content'])
itchat.send(msg=file, toUserName='filehelper')
os.remove(rev_tmp_dir + old_msg['msg_content'])
# 删除字典旧消息
msg_dict.pop(old_msg_id)
if __name__ == '__main__':
itchat.auto_login(hotReload=True,enableCmdQR=1)
itchat.run()
打开cmd,将文件拖入即可。
来源:https://blog.csdn.net/sunzhibin1/article/details/83348304
标签:python,微信,消息撤回
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
将pytorch的网络等转移到cuda
2023-08-10 08:33:46
Vue Element使用icon图标教程详解(第三方)
2023-07-02 16:28:43
![](https://img.aspxhome.com/file/2023/2/139672_0s.jpg)
Python实现二维数组按照某行或列排序的方法【numpy lexsort】
2022-03-13 05:06:31
详解如何在阿里云上安装mysql
2024-01-14 08:25:57
基于python使用Pillow做动态图在图中生成二维码以及图像处理
2022-12-29 16:33:03
![](https://img.aspxhome.com/file/2023/5/97235_0s.png)
安装PyInstaller失败问题解决
2022-03-18 04:21:41
![](https://img.aspxhome.com/file/2023/1/92431_0s.png)
Oracle轻松取得建表和索引的DDL语句
2009-02-26 10:26:00
利用JavaScript实现拖拽改变元素大小
2024-06-10 12:00:05
![](https://img.aspxhome.com/file/2023/2/56632_0s.png)
zabbix通过percona插件监控mysql的方法
2024-01-23 04:51:22
![](https://img.aspxhome.com/file/2023/5/93895_0s.png)
Python利用PyAutoGUI轻松搞定图片上传
2022-11-19 23:15:18
![](https://img.aspxhome.com/file/2023/1/97691_0s.jpg)
python字典的常用方法总结
2023-07-11 01:45:17
![](https://img.aspxhome.com/file/2023/4/107594_0s.jpg)
python threading和multiprocessing模块基本用法实例分析
2021-08-13 04:23:49
Pycharm-community-2020.2.3 社区版安装教程图文详解
2022-07-02 06:30:00
![](https://img.aspxhome.com/file/2023/3/133503_0s.png)
python 随机打乱 图片和对应的标签方法
2023-03-13 10:45:29
python3 实现爬取TOP500的音乐信息并存储到mongoDB数据库中
2024-01-21 06:11:12
![](https://img.aspxhome.com/file/2023/2/79632_0s.jpg)
Windows存储 SQL行溢出 差异备份及疑问
2008-12-24 15:22:00
Python 2种方法求某个范围内的所有素数(质数)
2022-09-07 08:43:40
![](https://img.aspxhome.com/file/2023/0/117990_0s.jpg)
python各种excel写入方式的速度对比
2021-04-23 22:30:15
PyQt 如何创建自定义QWidget
2023-09-13 17:30:48
去掉CSS赘余代码,CSS可以更简洁
2008-11-05 13:07:00