首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python:微信消息收集器

微信群人数限制是500人,如何突破限制,其中一个方法就是开发一个微信机器人作为传话筒,但本文不涉及这方面的内容,有兴趣者可自行修改。

itchat的原理,是模拟微信的网页版,登录成功后,通过解析与构造微信消息格式,达到交流目的。在此过程中, 可以将信息进行收集与处理。信息格式与微信公众号类似,有过公众号开发经验的,更可以快速上手。没有基础的, 也可以先了解微信消息格式,从而更好理解如何开发。

下面是一个收集公众号与微信群的例子。并将数据进行存储,一是存入一个本地数据库,二是存入本地日志文件。

# -*- coding: utf-8 -*-

"""

Created on Thu Jul 5 18:06:16 2018

@author: bunshinn

"""

import itchat

from itchat.content import TEXT

from itchat.content import *

import sys

import time

import re

import os

import sqlite3

import logging.handlers

class Logger(object):

def __init__(self, LOG_FILE='e:/wechat.log'):

self.LOG_FILE = LOG_FILE

self.formatter = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'

pass

def logger(self,logger_name, fmt='%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(levelname)s - %(message)s' ):

handler = logging.handlers.RotatingFileHandler(self.LOG_FILE,

maxBytes = 1024*1024,

backupCount = 5) # 实例化handler

formatter = logging.Formatter(fmt) # 实例化formatter

handler.setFormatter(formatter) # 为handler添加formatter

logger = logging.getLogger(logger_name) # 获取名为tst的logger

logger.addHandler(handler) # 为logger添加handler

logger.setLevel(logging.DEBUG)

return logger

logger = Logger().logger('wechat')

conn = sqlite3.connect('D:/workplace/wechatrecord.db')

c = conn.cursor()

face_bug=None #针对表情包的内容

msg_global = []

cnt = 0

@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO],isFriendChat=True, isGroupChat=True, isMpChat=True)

def handle_receive_msg(msg):

global cnt

cnt += 1

global face_bug

# 微信群消息

if msg['FromUserName'].count('@') >1:

memberList = itchat.update_chatroom(userName=msg['FromUserName'], detailedMember=True)

grp_msg_from = itchat.search_chatrooms(userName=msg['FromUserName'])

grp_nick_name =grp_msg_from['NickName']

actual_user_name = msg['ActualUserName']

actual_nick_name=''

actual_display_name=''

actual_remark_name=''

for user in memberList['MemberList']:

if user['UserName'] == actual_user_name:

actual_nick_name = user['NickName']

actual_display_name =user['DisplayName']

actual_remark_name = user['RemarkName']

break

msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

msg_id = msg['MsgId']

msg_from = msg['FromUserName']

msg_content = ''

msg_share_url=''

msg_time = msg['CreateTime']

if msg['Type'] == 'Text' or msg['Type'] == 'Friends': #如果发送的消息是文本或者好友推荐

msg_content = msg['Text']

#如果发送的消息是附件、视屏、图片、语音

elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \

or msg['Type'] == 'Picture' \

or msg['Type'] == 'Recording':

msg_content = msg['FileName'] #内容就是他们的文件名

# 文件下载

msg.download('e:/wechatfiles/'+str(msg_time)+'_'+msg.fileName)

elif msg['Type'] == 'Card': #如果消息是推荐的名片

msg_content = msg['RecommendInfo']['NickName'] + '的名片' #内容就是推荐人的昵称和性别

if msg['RecommendInfo']['Sex'] == 1:

msg_content += '性别为男'

else:

msg_content += '性别为女'

elif msg['Type'] == 'Map': #如果消息为分享的位置信息

x, y, location = re.search(

"

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'] #记录分享的url

face_bug=msg_content

sql = '''insert into records (msg_id,

msg_type,

msg_from,

grp_nick_name,

actual_user_name,

actual_nick_name,

actual_display_name,

actual_remark_name,

msg_content,

msg_time,

msg_time_rec,

msg_share_url) values("{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}");'''

record = (msg_id, msg['Type'], msg_from, grp_nick_name,actual_user_name,actual_nick_name,actual_display_name,

actual_remark_name, msg_content, str(msg_time),msg_time_rec, msg_share_url)

print(record)

logger.info('#|#'.join(record))

c.execute(sql.format(*record))

if cnt % 5 == 0:

conn.commit()

itchat.auto_login(hotReload=True,enableCmdQR=True)

itchat.run()

更多功能,如发送信息等等,可自行构造消息结构,通过send系列函数即可实现。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180708G1976000?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券