python-itchat 统计微信群、好友数量,及原始消息数据

参考来自:https://itchat.readthedocs.io/zh/latest/api/

#coding=utf-8
import itchat
from itchat.content import TEXT
from itchat.content import *
import sys
import time
import re
reload(sys)
sys.setdefaultencoding('utf8')
import os

@itchat.msg_register([TEXT,PICTURE,FRIENDS,CARD,MAP,SHARING,RECORDING,ATTACHMENT,VIDEO],isGroupChat=True)
def receive_msg(msg):
    groups  = itchat.get_chatrooms(update=True)
    friends = itchat.get_friends(update=True)
    print "群数量:",len(groups)
    for i in range(0,len(groups)):
        print i+1,"--",groups[i]['NickName'],groups[i]['MemberCount'],"人"
    print "好友数量",len(friends)-1
    for f in range(1,len(friends)):#第0个好友是自己,不统计
        if friends[f]['RemarkName']: # 优先使用好友的备注名称,没有则使用暱称
            user_name = friends[f]['RemarkName']
        else:
            user_name = friends[f]['NickName']
        sex = friends[f]['Sex']
        print f,"--",user_name,sex
itchat.auto_login(hotReload=True)
itchat.run()

效果:
这里写图片描述

好友:

# 获取自己的用户信息,返回自己的属性字典
itchat.search_friends()
# 获取特定UserName的用户信息
itchat.search_friends(userName='@abcdefg1234567')
# 获取任何一项等于name键值的用户
itchat.search_friends(name='wxceshi')
# 获取分别对应相应键值的用户
itchat.search_friends(wechatAccount='wceshi')
# 三、四项功能可以一同使用
itchat.search_friends(name='wxceshi', wechatAccount='wcceshi')

公众号:

公众号的获取方法为get_mps,将会返回完整的公众号列表。
其中每个公众号为一个字典
传入update键为True将可以更新公众号列表并返回
import  itchat
itchat.auto_login(hotReload=True)

mpsList=itchat.get_mps(update=True)[1:]

total=0
for it in mpsList:
    print(it['NickName']+':'+it['Signature'])
    total=total+1

print('公众号的数目是%d'%total)

公众号的搜索方法为search_mps,有两种搜索方法: 
1. 获取特定UserName的公众号 
2. 获取名字中含有特定字符的公众号
如果两项都做了特定,将会仅返回特定UserName的公众号,下面是示例程序:
# 获取特定UserName的公众号,返回值为一个字典
itchat.search_mps(userName='@abcdefg1234567')
# 获取名字中含有特定字符的公众号,返回值为一个字典的列表
itchat.search_mps(name='gzh')
# 以下方法相当于仅特定了UserName
itchat.search_mps(userName='@abcdefg1234567', name='gzh')

群聊:

群聊的获取方法为get_chatrooms,将会返回完整的群聊列表。
其中每个群聊为一个字典
传入update键为True将可以更新群聊列表并返回通讯录中保存的群聊列表
    群聊列表为后台自动更新,如果中途意外退出存在极小的概率产生本地群聊消息与后台不同步
    为了保证群聊信息在热启动中可以被正确的加载,即使不需要持续在线的程序也需要运行itchat.run()
    如果不想要运行上述命令,请在退出程序前调用-itchat.dump_login_status(),更新热拔插需要的信息

import  itchat
itchat.auto_login(hotReload=True)

#itchat.run()

mpsList=itchat.get_chatrooms(update=True)[1:]

total=0
for it in mpsList:
    print(it['NickName'])
    total=total+1

print('群聊的数目是%d'%total)

#显示所有的群聊,包括未保存在通讯录中的,如果去掉则只是显示在通讯录中保存的
itchat.dump_login_status()
群聊的搜索方法为search_chatrooms,有两种搜索方法: 1. 获取特定UserName的群聊 2. 获取名字中含有特定字符的群聊
如果两项都做了特定,将会仅返回特定UserName的群聊,下面是示例程序:
# 获取特定UserName的群聊,返回值为一个字典
itchat.search_chatrooms(userName='@abcdefg1234567')
# 获取名字中含有特定字符的群聊,返回值为一个字典的列表
itchat.search_chatrooms(name='LittleCoder')
# 以下方法相当于仅特定了UserName
itchat.search_chatrooms(userName='@abcdefg1234567', name='LittleCoder')
群聊用户列表的获取方法为update_chatroom。

    群聊在首次获取中不会获取群聊的用户列表,所以需要调用该命令才能获取群聊的成员
    该方法需要传入群聊的UserName,返回特定群聊的用户列表
memberList = itchat.update_chatroom('bcdefg67')
创建群聊、增加、删除群聊用户的方法如下所示:

    由于之前通过群聊检测是否被好友拉黑的程序,目前这三个方法都被严格限制了使用频率
    删除群聊需要本账号为群管理员,否则会失败
    将用户加入群聊有直接加入与发送邀请,通过useInvitation设置
    超过40人的群聊无法使用直接加入的加入方式,特别注意
memberList = itchat.get_friends()[1:]
# 创建群聊,topic键值为群聊名
chatroomUserName = itchat.create_chatroom(memberList, 'test chatroom')
# 删除群聊内的用户
itchat.delete_member_from_chatroom(chatroomUserName, memberList[0])
# 增加用户进入群聊
itchat.add_member_into_chatroom(chatroomUserName, memberList[0], useInvitation=False)

消息的基础数据:

群基础信息:列表,每个元素是一个群,字典,列表长度就是群的数量.

UserName -- @@410e35039bc309eaa37e444fc932cf1f0d11b6e79d9eff610fc971701940778b
City -- 
MemberList -- [{u'UserName': u'@2a1f4757bbedbbc1c58be274655e7e69823fcf8288bb50aa0ec9769698b883f5', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, u'RemarkPYInitial': u'', u'PYQuanPin': u'', u'MemberStatus': 0, u'NickName': u'\u82b1\u82e5\u96e8', u'AttrStatus': 233509}, {u'UserName': u'@91271c0895c75b4290c4d71673040978b50c1d81005b768728497bbcfc9657f3', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, u'RemarkPYInitial': u'', u'PYQuanPin': u'', u'MemberStatus': 0, u'NickName': u'alise', u'AttrStatus': 235617}, {u'UserName': u'@6151801ec2a6333e7fd5530f812c931e14d9220477756796013ae3cbab54b64f', u'RemarkPYQuanPin': u'', u'DisplayName': u'\u81f3\u5c0a\u7389-\u5c0f\u9e1f\u98de', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, u'RemarkPYInitial': u'', u'PYQuanPin': u'', u'MemberStatus': 0, u'NickName': u'\u81f3\u5c0a\u7389', u'AttrStatus': 102525}]

VerifyFlag -- 0
Province -- 
KeyWord -- 
RemarkName -- 
self -- {u'UserName': u'@2a1f4757bbedbbc1c58be274655e7e69823fcf8288bb50aa0ec9769698b883f5', u'RemarkPYQuanPin': u'', u'DisplayName': u'', u'KeyWord': u'', u'PYInitial': u'', u'Uin': 0, u'RemarkPYInitial': u'', u'PYQuanPin': u'', u'MemberStatus': 0, u'NickName': u'\u82b1\u82e5\u96e8', u'AttrStatus': 233509}
isAdmin -- None
ContactType -- 0
HideInputBarFlag -- 0
AttrStatus -- 0
SnsFlag -- 0
MemberCount -- 3
OwnerUin -- 0
Alias -- 
Signature -- 
ContactFlag -- 2
NickName -- 一只小鸟飞
ChatRoomOwner -- @2a1f4757bbedbbc1c58be274655e7e69823fcf8288bb50aa0ec9769698b883f5
HeadImgUrl -- /cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=0&username=@@410e35039bc309eaa37e444fc932cf1f0d11b6e79d9eff610fc971701940778b&skey=@crypt_f707bac_06ef94d1305fd1ebf9192f58bdee290c
Sex -- 0
Statues -- 1
HeadImgUpdateFlag -- 1

好友基础信息:列表,每个元素是一个好友字典,列表长度即好友数量。(自己是第0个好友)

UserName -- @6151801ec2a6333e7fd5530f812c931e14d9220477756796013ae3cbab54b64f
City -- 朝阳
DisplayName -- 
UniFriend -- 0
MemberList -- []
PYQuanPin -- zhizunyu
RemarkPYInitial -- ZZYBZ
Sex -- 1
AppAccountFlag -- 0
VerifyFlag -- 0
Province -- 北京
KeyWord -- 
RemarkName -- 至尊玉备注
PYInitial -- ZZY
IsOwner -- 0
ChatRoomId -- 0
ContactType -- 0
HideInputBarFlag -- 0
EncryChatRoomId -- 
AttrStatus -- 102525
SnsFlag -- 17
MemberCount -- 0
OwnerUin -- 0
Alias -- 
Signature -- 本来无一物,何处惹尘埃。
ContactFlag -- 3
NickName -- 至尊玉
ChatRoomOwner -- 
RemarkPYQuanPin -- zhizunyubeizhu
HeadImgUrl -- /cgi-bin/mmwebwx-bin/webwxgeticon?seq=656993295&username=@6151801ec2a6333e7fd5530f812c931e14d9220477756796013ae3cbab54b64f&skey=@crypt_f707bac_06ef94d1305fd1ebf9192f58bdee290c
Uin -- 0
StarFriend -- 0
Statues -- 0
HeadImgUpdateFlag -- 1

好友消息:每条消息是一个字典。消息内容:msg['Content']

AppInfo -- {u'Type': 0, u'AppID': u''}
ImgWidth -- 0
FromUserName -- @6151801ec2a6333e7fd5530f812c931e14d9220477756796013ae3cbab54b64f
PlayLength -- 0
OriContent -- 
ImgStatus -- 1
RecommendInfo -- {u'UserName': u'', u'Province': u'', u'City': u'', u'Scene': 0, u'QQNum': 0, u'Content': u'', u'Alias': u'', u'OpCode': 0, u'Signature': u'', u'Ticket': u'', u'Sex': 0, u'NickName': u'', u'AttrStatus': 0, u'VerifyFlag': 0}
Content -- This is friend msg
MsgType -- 1
ImgHeight -- 0
StatusNotifyUserName -- 
StatusNotifyCode -- 0
Type -- Text
NewMsgId -- 4967860504982482776
Status -- 3
VoiceLength -- 0
MediaId -- 
MsgId -- 4967860504982482776
ToUserName -- @2a1f4757bbedbbc1c58be274655e7e69823fcf8288bb50aa0ec9769698b883f5
ForwardFlag -- 0
FileName -- 
Url -- 
HasProductId -- 0
FileSize -- 
AppMsgType -- 0
Text -- This is friend msg
Ticket -- 
CreateTime -- 1515398261
EncryFileName -- 
SubMsgType -- 0

群聊消息:
ActualNickName -- 至尊玉-小鸟飞  #用户在群内的暱称
AppInfo -- {u'Type': 0, u'AppID': u''}
ImgWidth -- 0
FromUserName -- @@410e35039bc309eaa37e444fc932cf1f0d11b6e79d9eff610fc971701940778b #来个哪个群聊
PlayLength -- 0
OriContent -- 
ImgStatus -- 1
RecommendInfo -- {u'UserName': u'', u'Province': u'', u'City': u'', u'Scene': 0, u'QQNum': 0, u'Content': u'', u'Alias': u'', u'OpCode': 0, u'Signature': u'', u'Ticket': u'', u'Sex': 0, u'NickName': u'', u'AttrStatus': 0, u'VerifyFlag': 0}
Content -- This is a group msg
MsgType -- 1
CreateTime -- 1515398528
ImgHeight -- 0
StatusNotifyUserName -- 
StatusNotifyCode -- 0
Type -- Text
NewMsgId -- 4737322597592466590
Status -- 3
VoiceLength -- 0
MediaId -- 
MsgId -- 4737322597592466590
ToUserName -- @2a1f4757bbedbbc1c58be274655e7e69823fcf8288bb50aa0ec9769698b883f5 #发给自己的
ForwardFlag -- 0
FileName -- 
Url -- 
HasProductId -- 0
FileSize -- 
AppMsgType -- 0
Text -- This is a group msg
ActualUserName -- @6151801ec2a6333e7fd5530f812c931e14d9220477756796013ae3cbab54b64f #谁发的消息
Ticket -- 
isAt -- False
EncryFileName -- 
SubMsgType -- 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章