Windows下用python来获取微信撤回消息
转自:https://blog.csdn.net/sunzhibin1/article/details/83348304
娱乐(windows系统)
安装itchat
itchat是一个开源的python微信库,支持发送消息、图片、视频、地图、名片、文件等,还可以实现自动回复等多种功能。
看到的一个文档还不错 https://segmentfault.com/a/1190000009420701
1.安装python3.6
2.安装成功之后,进入pip3.exe所在的目录
一般在安装目录的script目录下:C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts
安装itchat:pip3.exe install itchat 下图我的已经安装过了(注意:使用3以及以上版本执行)。
代码
新建一个wx.py,编辑以下内容
- # -*-encoding:utf--*-
- 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: -- ::
- 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(, , )
- 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()
- old_msg = msg_dict.get(old_msg_id, {})
- if len(old_msg_id) < :
- 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=)
- itchat.run()
执行
扫码登录
你就可以在文件传输助手中查看到撤回的微信了
Windows下用python来获取微信撤回消息的更多相关文章
- Windows下使用python库 curses遇到错误消息的解决方案
在Windows系统下执行python应用时,有时会遇到错误信息: ModuleNotFoundError: No module named '_curses'. 然而查看Windows系统里pyth ...
- 【转】linux和windows下安装python集成开发环境及其python包
本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...
- windows下安装python模块
如何在windows下安装python模块 1. 官网下载安装包,比如(pip : https://pypi.python.org/pypi/pip#downloads) pip-9.0.1.tar. ...
- windows下安装python和依赖包的利器——Anaconda
在windows下安装python和很多依赖包,安装起来略为痛苦,可以使用python的大整合包——Anaconda Anaconda下载地址: http://continuum.io/downloa ...
- 详细介绍windows下使用python pylot进行网站压力测试
windows下使用python进行网站压力测试,有两个必不可少的程序需要安装,一个是python,另一个是pylot.python是一个安装软件,用来运行python程序,而pylot则是pytho ...
- windows下创建Python虚拟环境
windows下创建Python虚拟环境 说明 由于Python的版本众多,还有Python2和Python3的争论,因此有些软件包或第三方库就容易出现版本不兼容的问题. 通过 virtualenv ...
- Windows下的Python安装与环境变量的配置
Windows下的Python安装与环境变量的配置 第一步:python下载: Python安装包下载地址:http://www.python.org/ 第二步:python安装: 双击下载包,进入P ...
- Windows下安装Python虚拟环境
Windows下安装Python虚拟环境 虚拟环境安装 需求概要 "虚拟环境"是从电脑独立开辟出来的环境.就好比我们生活中的橱柜中,会把酱油放在一个瓶子里,把醋放在另外一个瓶子里, ...
- Windows下安装python的scipy等科学计算包(转)
如果要使用python进行科学计算.数据分析等,一定要安装scipy.seaborn.numpy等等包. 但Windows下安装python的第三方库经常会出现问题.此前,已介绍过Windows下如何 ...
随机推荐
- 小tip: transition与visibility
一.transition与visibility 这里的transition指的就是CSS3中的那个过渡动画属性transition. 如果我们仔细查看其支持的CSS属性值,会发现竟然有一个visibi ...
- iTextSharpH
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- awt多线程聊天
public class ChatServer { boolean started = false; ServerSocket serverSocket = null; public void sta ...
- PE、PB、PEG三大估值法的正确使用方法!
目前市面上的估值方法有很多,比如PE估值法.PB估值法.PEG估值法,但是我相信,真正会用的人并不多,比如说目前动态市盈率121倍的比亚迪真的高估吗?比如目前市净率为0.63倍的众泰汽车真的是破净股吗 ...
- 【转】前端框架天下三分:Angular React 和 Vue的比较
前端框架天下三分:Angular React 和 Vue的比较 原文链接:http://blog.csdn.net/haoshidai/article/details/52346865 前端这几年的技 ...
- c语言实现wc功能
本随笔对网站http://blog.chinaunix.net/uid-22566367-id-381958.html有所借鉴 #include <stdio.h> #define BEG ...
- office 2016密钥
Office 2016 Pro Plus Retail 版激活密钥:[Key]:3XJTG-YNBMY-TBH9M-CWB2Y-YWRHH[Key]:6TCQ3-NBBJ2-RTJCM-HFRKV-G ...
- android从IIS/asp.net下载apk文件
解决步骤: 1.web.config中 <configuration> <configSections> ... <section name="rewr ...
- File 文件
1 File 概述 文件:file目录:directory路径:path File类静态成员变量: pathSeparator:与系统有关的路径分隔符,为了方便,它被表示为一个字符串separator ...
- pwn学习日记Day4 基础知识积累
知识杂项 *:字符串重复 空指令NOP:\x90 cmp:是比较指令,cmp的功能相当于减法指令.它不保存结果,只是影响相应的标志位. xor:将两个操作数进行异或运算,并将结果存放到操作数1中. s ...