脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python-itchat 统计微信群、好友数量,及原始消息数据的实例

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

2021-05-31 00:30zhizunyu2009 Python

今天小编就为大家分享一篇python-itchat 统计微信群、好友数量,及原始消息数据的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

python" id="highlighter_633141">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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()

效果:

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

好友:

?
1
2
3
4
5
6
7
8
9
10
# 获取自己的用户信息,返回自己的属性字典
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')

公众号:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
公众号的获取方法为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')

群聊:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
群聊的获取方法为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)

消息的基础数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
群基础信息:列表,每个元素是一个群,字典,列表长度就是群的数量.
 
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

以上这篇python-itchat 统计微信群、好友数量,及原始消息数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zhizunyu2009/article/details/79000190

延伸 · 阅读

精彩推荐