当前位置:首页 > 网站源码 > 正文

微信转发软件源码丨微信转发文章源码

微信转发软件源码丨微信转发文章源码

前言python的强大在于丰富的类库,经常会看到几行代码就可以实现非常强大的功能。它可以做爬虫、AI、自动化测试、小工具(抢票、抓包、微信消息抓取)等等。本次我...

前言

python的强大在于丰富的类库,经常会看到几行代码就可以实现非常强大的功能。它可以做爬虫、AI、自动化测试、小工具(抢票、抓包、微信消息抓取)等等。

本次我们来讲讲怎么来抓取微信消息?抓取微信微信消息以后我们可以做什么?

废话不多说,直接进正题~

思路

一、引用的类库

#日志import logging#正则import re#shutil模块主要作用与拷贝文件用的import shutil#基于网页版微信的python微信APIimport itchat#抓取消息的类型库from itchat.content import *#pymysql来MySQL数据库的连接import pymysql

二、登录微信

hotReload=True可以缓存登录信息,防止重复登录。登录以后会在本地生成一个itchat.pkl文件.

def main(): #登录 记录登录状态 防止重复登录 itchat.auto_login(hotReload=True) itchat.run()if __name__ == '__main__': main()

三、接收消息

处理好友的文本类消息(文本、位置、名片、通知、分享),获取消息并保存数据库。用户的撤回的消息转发到文件助手,再也不怕看不到撤回的消息了.

#创建字典storage = dict()# 处理文本类消息# 包括文本、位置、名片、通知、分享@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])def simple_reply(msg): try: #声明全局变量 global storage output_info('simple_reply接收到的消息是:%s' % msg) oldMsgId = '' oldContent='' #获取消息id msgId = msg['MsgId'] #获取消息内容,去除首位空格 content = msg['Text'].strip() #获取消息类型 type = msg['Type'] #撤回消息转发到文件传输助手 if msg['Type'] == 'Note': #获取撤回的消息id oldMsgId = re.search('<msgid>(.*?)</msgid>', msg['Content']).group(1) #字典中获取撤回消息的内容 oldContent = storage[oldMsgId]['content'] tip = storage[oldMsgId]['name'] '撤回一条消息:' oldContent #转发到微信文件助手 itchat.send(tip,'filehelper') #非撤回消息 #找出谁发的消息 fromUserName = msg['FromUserName'] #如果不是腾讯推送的新闻,获取发送消息的用户微信名称 if fromUserName != 'newsapp': fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName'] toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName'] #保存到字典 storage[msgId] = {'name':fromUserName,'content':content} #保存到数据库 add_msg(msgId,oldMsgId,fromUserName,toUserName,content,oldContent,type,'01','') except Exception as e: output_info('simple_reply处理消息异常:%s' % e)

处理好友的多媒体类消息(包括图片、录音、文件、视频),获取的文件下载到本地,并在数据库中记录文件信息。

# 处理多媒体类消息# 包括图片、录音、文件、视频 @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])def download_files(msg): try: global storage output_info('download_files接收到的消息是:%s' % msg) msgId = msg['MsgId'] type = msg['Type'] #找出谁发的消息 fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName'] toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName'] #文件保存的路径 fileName = "weChatFile/" msg['FileName'] #把消息放入字典 storage[msgId] = {'name':fromUserName,'content':fileName} #保存消息 add_msg(msgId,"",fromUserName,toUserName,fileName,'',type,'01','') #msg['Text']是一个文件下载函数 #先将文件下载下来 msg['Text'](msg['FileName']) output_info('download_files正在保存文件:%s' % fileName) #文件下载到本地 shutil.move(msg['FileName'], fileName) print("文件:" fileName "保存成功!") output_info('download_files文件:%s:' fileName '保存成功!') except Exception as e: output_info('download_files处理文件异常:%s' % e)

处理群里面的文本类消息(文本、位置、名片、通知、分享)

# 处理文本类消息# 包括文本、位置、名片、通知、分享#isGroupChat = True接受群消息@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING], isGroupChat = True)def groupchat_reply(msg): try: #打印接收到的消息 output_info('groupchat_reply接收到的~~消息是:%s' % msg) #获取消息id msgId = msg['MsgId'] #获取消息类型 type = msg['Type'] oldContent = "" oldMsgId = "" groupName = "" toUserName = "" fromUserName = "" #获取消息来源 userName=msg['FromUserName'] #@@是群转过来的消息 if userName.find('@@') !=-1: #获取群名称 groupName = itchat.search_chatrooms(userName=msg['FromUserName'])['NickName'] #获取说话人的微信名 fromUserName = msg['ActualNickName'] #获取接收微信的用户信息 toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName'] else: groupName = itchat.search_chatrooms(userName=msg['ToUserName'])['NickName'] fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName'] toUserName = groupName if msg['Type'] == 'Note': oldMsgId = re.search('<msgid>(.*?)</msgid>', msg['Content']).group(1) oldContent = storage[oldMsgId]['content'] tip = storage[oldMsgId]['name'] '撤回一条群消息:' oldContent itchat.send(tip,'filehelper') content = msg['Text'].strip() storage[msgId] = {'name':fromUserName,'content':content} add_msg(msgId,oldMsgId,fromUserName,toUserName,str(content),oldContent,type,'02',str(groupName)) # if msg['isAt']: # itchat.send(u'@%su2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName']) except Exception as e: output_info('groupchat_reply处理群里的消息异常:%s' % e)

处理群里面的多媒体类消息(包括图片、录音、文件、视频)

# 处理多媒体类消息

# 包括图片、录音、文件、视频@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO], isGroupChat = True)def groupchat_files(msg): try: output_info('groupchat_files接收到的~~群~~消息是:%s' % msg) msgId = msg['MsgId'] type = msg['Type'] groupName = "" toUserName = "" fromUserName = "" userName=msg['FromUserName'] #@@是群转过来的消息 if userName.find('@@') !=-1: groupName = itchat.search_chatrooms(userName=msg['FromUserName'])['NickName'] fromUserName = msg['ActualNickName'] toUserName = itchat.search_friends(userName=msg['ToUserName'])['NickName'] else: groupName = itchat.search_chatrooms(userName=msg['ToUserName'])['NickName'] fromUserName = itchat.search_friends(userName=msg['FromUserName'])['NickName'] toUserName = groupName fileName = "weChatGroupFile/" msg['FileName'] storage[msgId] = {'name':fromUserName,'content':fileName} output_info("groupchat_files正在保存文件:%s" fileName) add_msg(msgId,"",fromUserName,toUserName,fileName,'',type,'01','') msg['Text'](msg['FileName']) #移动 shutil.move(msg['FileName'], fileName) output_info('groupchat_files文件:%s:' fileName '保存成功!') # if msg['isAt']: # itchat.send(u'@%su2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName']) except Exception as e: output_info('groupchat_files处理群里的消息异常:%s' % e)

四、保存数据

直接上源码,都可以看懂。

#添加数据库 def add_msg(msgId,oldMsgId,fromUserName,toUserName,content,oldContent,type,source,groupName): try: #链接数据库 conn = pymysql.connect(host='localhost', user = "root", passwd="root", db="wechat", port=3306, charset="utf8mb4") # 使用 cursor() 方法创建一个游标对象 cursor #用来获得python执行Mysql命令的方法 cursor = conn.cursor() sql = "INSERT INTO `ge_wechat_msg` (`id`, `msgId`, `oldMsgId`, `fromUserName`, `toUserName`,`content`, `oldContent`, `type`, `source`,`groupName`, `makedate`, `modifydate`) VALUES (NULL, '" msgId "', '" oldMsgId "', '" fromUserName "','" toUserName "','" content "', '" oldContent "', '" type "','" source "','" groupName "', now(), NOW())" count = cursor.execute(sql) #判断是否成功 if count > 0: output_info('添加数据成功') #提交事务 conn.commit() except Exception as e: conn.rollback() output_info('添加数据库异常:%s' % e)

五、打印日志

logging.basicConfig(filename='../LOG/weChat02.log',format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]', level = logging.INFO,filemode='a',datefmt='%Y-%m-%d%I:%M:%S %p')def output_info(msg): try: print('[INFO] %s' % msg) logging.info('u' msg) except Exception as e: print('记录日志异常:' e)

总结:

上面就是全部代码了,那么接收到微信消息,我们可以做什么呢?

对接图灵机器人,再也不怕打游戏,没时间和女朋友聊天了(祈祷别被发现)。

可以给朋友或者给群聊一键群发消息。

对于微商来说,会有非常多的群,所以python可以帮你管理群聊,分析用户行为。

等等~~~

需要源码可以私信联系~~

发表评论

推荐文章

  • sem开户(sem推广开户渠道)sem开户(sem推广开户渠道)
  • 炫酷花里胡哨倒计时引导页HTML源码炫酷花里胡哨倒计时引导页HTML源码
  • SEO网站推广怎么做(SEO网站推广的6个方法与技巧)SEO网站推广怎么做(SEO网站推广的6个方法与技巧)
  • 最新文章