爬虫应用——微信防撤回
小编最近学习到了微信防撤回的代码,拿来分享一下吧!!
import itchat (第三方库,需要导入)
import re #导入正则表达式(不用导入)
#import itchat
import re #导入正则表达式
# 消息注册机制
# 接收到消息就调用下面的函数
storage = dict()
@itchat.msg_register([‘Text’])
def message(msg):
global storage # 声明
print(msg)
# 提取文本类的消息
# 把所有的文本类消息用字典的形式存储起来
id = msg[‘MsgId’]
content = msg[‘Text’].strip()
# 找出是谁发的消息,朋友的昵称
name = itchat.search_friends(userName=msg[‘FromUserName’])[‘NickName’]
storage[id] = {‘name’: name, ‘content’: content}
# 只要接收到通知消息就调用下方的函数
@itchat.msg_register(‘Note’)
def notice(msg):
print(msg)
if ‘撤回了一条消息’ in msg[‘Text’]:
old_msg_id = re.search(’<msgid>(.*?)</msgid>’,msg[‘Content’]).group(1)
print(old_msg_id)
# 找出是谁发的
name = storage[old_msg_id][‘name’]
# 找出消息的内容
content = storage[old_msg_id][‘content’]
tip = name +" 撤回了一条消息 " + content
# 发送一个微信消息
itchat.send(‘tip’, ‘filehelper’)
def main():
itchat.auto_login()
# 让微信运行起来
itchat.run()
调用函数,一旦被用作函数调用,则执行下面的程序:
if name==“main”:
main() 消息注册机制
不过小编在登陆网页时出现了 不能登陆的问题 ???
有没有 哪位大神知道拿错了呢?????