python-itchat 統(tǒng)計(jì)微信群、好友數(shù)量,及原始消息數(shù)據(jù)的實(shí)例
參考來(lái)自: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 "群數(shù)量:",len(groups) for i in range(0,len(groups)): print i+1,"--",groups[i]['NickName'],groups[i]['MemberCount'],"人" print "好友數(shù)量",len(friends)-1 for f in range(1,len(friends)):#第0個(gè)好友是自己,不統(tǒng)計(jì) if friends[f]['RemarkName']: # 優(yōu)先使用好友的備注名稱,沒(méi)有則使用昵稱 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') # 獲取任何一項(xiàng)等于name鍵值的用戶 itchat.search_friends(name='wxceshi') # 獲取分別對(duì)應(yīng)相應(yīng)鍵值的用戶 itchat.search_friends(wechatAccount='wceshi') # 三、四項(xiàng)功能可以一同使用 itchat.search_friends(name='wxceshi', wechatAccount='wcceshi')
公眾號(hào):
公眾號(hào)的獲取方法為get_mps,將會(huì)返回完整的公眾號(hào)列表。 其中每個(gè)公眾號(hào)為一個(gè)字典 傳入update鍵為T(mén)rue將可以更新公眾號(hào)列表并返回 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('公眾號(hào)的數(shù)目是%d'%total) 公眾號(hào)的搜索方法為search_mps,有兩種搜索方法: 1. 獲取特定UserName的公眾號(hào) 2. 獲取名字中含有特定字符的公眾號(hào) 如果兩項(xiàng)都做了特定,將會(huì)僅返回特定UserName的公眾號(hào),下面是示例程序: # 獲取特定UserName的公眾號(hào),返回值為一個(gè)字典 itchat.search_mps(userName='@abcdefg1234567') # 獲取名字中含有特定字符的公眾號(hào),返回值為一個(gè)字典的列表 itchat.search_mps(name='gzh') # 以下方法相當(dāng)于僅特定了UserName itchat.search_mps(userName='@abcdefg1234567', name='gzh')
群聊:
群聊的獲取方法為get_chatrooms,將會(huì)返回完整的群聊列表。 其中每個(gè)群聊為一個(gè)字典 傳入update鍵為T(mén)rue將可以更新群聊列表并返回通訊錄中保存的群聊列表 群聊列表為后臺(tái)自動(dòng)更新,如果中途意外退出存在極小的概率產(chǎn)生本地群聊消息與后臺(tái)不同步 為了保證群聊信息在熱啟動(dòng)中可以被正確的加載,即使不需要持續(xù)在線的程序也需要運(yùn)行itchat.run() 如果不想要運(yùn)行上述命令,請(qǐng)?jiān)谕顺龀绦蚯罢{(diào)用-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('群聊的數(shù)目是%d'%total) #顯示所有的群聊,包括未保存在通訊錄中的,如果去掉則只是顯示在通訊錄中保存的 itchat.dump_login_status() 群聊的搜索方法為search_chatrooms,有兩種搜索方法: 1. 獲取特定UserName的群聊 2. 獲取名字中含有特定字符的群聊 如果兩項(xiàng)都做了特定,將會(huì)僅返回特定UserName的群聊,下面是示例程序: # 獲取特定UserName的群聊,返回值為一個(gè)字典 itchat.search_chatrooms(userName='@abcdefg1234567') # 獲取名字中含有特定字符的群聊,返回值為一個(gè)字典的列表 itchat.search_chatrooms(name='LittleCoder') # 以下方法相當(dāng)于僅特定了UserName itchat.search_chatrooms(userName='@abcdefg1234567', name='LittleCoder') 群聊用戶列表的獲取方法為update_chatroom。 群聊在首次獲取中不會(huì)獲取群聊的用戶列表,所以需要調(diào)用該命令才能獲取群聊的成員 該方法需要傳入群聊的UserName,返回特定群聊的用戶列表 memberList = itchat.update_chatroom('bcdefg67') 創(chuàng)建群聊、增加、刪除群聊用戶的方法如下所示: 由于之前通過(guò)群聊檢測(cè)是否被好友拉黑的程序,目前這三個(gè)方法都被嚴(yán)格限制了使用頻率 刪除群聊需要本賬號(hào)為群管理員,否則會(huì)失敗 將用戶加入群聊有直接加入與發(fā)送邀請(qǐng),通過(guò)useInvitation設(shè)置 超過(guò)40人的群聊無(wú)法使用直接加入的加入方式,特別注意 memberList = itchat.get_friends()[1:] # 創(chuàng)建群聊,topic鍵值為群聊名 chatroomUserName = itchat.create_chatroom(memberList, 'test chatroom') # 刪除群聊內(nèi)的用戶 itchat.delete_member_from_chatroom(chatroomUserName, memberList[0]) # 增加用戶進(jìn)入群聊 itchat.add_member_into_chatroom(chatroomUserName, memberList[0], useInvitation=False)
消息的基礎(chǔ)數(shù)據(jù):
群基礎(chǔ)信息:列表,每個(gè)元素是一個(gè)群,字典,列表長(zhǎng)度就是群的數(shù)量. 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 -- 一只小鳥(niǎo)飛 ChatRoomOwner -- @2a1f4757bbedbbc1c58be274655e7e69823fcf8288bb50aa0ec9769698b883f5 HeadImgUrl -- /cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=0&username=@@410e35039bc309eaa37e444fc932cf1f0d11b6e79d9eff610fc971701940778b&skey=@crypt_f707bac_06ef94d1305fd1ebf9192f58bdee290c Sex -- 0 Statues -- 1 HeadImgUpdateFlag -- 1 好友基礎(chǔ)信息:列表,每個(gè)元素是一個(gè)好友字典,列表長(zhǎng)度即好友數(shù)量。(自己是第0個(gè)好友) UserName -- @6151801ec2a6333e7fd5530f812c931e14d9220477756796013ae3cbab54b64f City -- 朝陽(yáng) 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 -- 本來(lái)無(wú)一物,何處惹塵埃。 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 好友消息:每條消息是一個(gè)字典。消息內(nèi)容: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 -- 至尊玉-小鳥(niǎo)飛 #用戶在群內(nèi)的昵稱 AppInfo -- {u'Type': 0, u'AppID': u''} ImgWidth -- 0 FromUserName -- @@410e35039bc309eaa37e444fc932cf1f0d11b6e79d9eff610fc971701940778b #來(lái)個(gè)哪個(gè)群聊 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 #發(fā)給自己的 ForwardFlag -- 0 FileName -- Url -- HasProductId -- 0 FileSize -- AppMsgType -- 0 Text -- This is a group msg ActualUserName -- @6151801ec2a6333e7fd5530f812c931e14d9220477756796013ae3cbab54b64f #誰(shuí)發(fā)的消息 Ticket -- isAt -- False EncryFileName -- SubMsgType -- 0
以上這篇python-itchat 統(tǒng)計(jì)微信群、好友數(shù)量,及原始消息數(shù)據(jù)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 基于Python數(shù)據(jù)分析之pandas統(tǒng)計(jì)分析
- python實(shí)現(xiàn)的分析并統(tǒng)計(jì)nginx日志數(shù)據(jù)功能示例
- Python數(shù)據(jù)可視化 pyecharts實(shí)現(xiàn)各種統(tǒng)計(jì)圖表過(guò)程詳解
- Python實(shí)現(xiàn)Mysql數(shù)據(jù)統(tǒng)計(jì)及numpy統(tǒng)計(jì)函數(shù)
- Python如何獲得百度統(tǒng)計(jì)API的數(shù)據(jù)并發(fā)送郵件示例代碼
- Python數(shù)據(jù)分析之雙色球統(tǒng)計(jì)兩個(gè)紅和藍(lán)球哪組合比例高的方法
- Python數(shù)據(jù)分析之雙色球統(tǒng)計(jì)單個(gè)紅和藍(lán)球哪個(gè)比例高的方法
- Python數(shù)據(jù)分析之雙色球中藍(lán)紅球分析統(tǒng)計(jì)示例
- Python實(shí)現(xiàn)讀寫(xiě)sqlite3數(shù)據(jù)庫(kù)并將統(tǒng)計(jì)數(shù)據(jù)寫(xiě)入Excel的方法示例
- 用python實(shí)現(xiàn)簡(jiǎn)單EXCEL數(shù)據(jù)統(tǒng)計(jì)的實(shí)例
- Python 數(shù)據(jù)的累加與統(tǒng)計(jì)的示例代碼
相關(guān)文章
Python實(shí)現(xiàn)自動(dòng)化郵件發(fā)送過(guò)程詳解
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)自動(dòng)化郵件發(fā)送,可以讓你擺脫繁瑣的重復(fù)性業(yè)務(wù),可以節(jié)省非常多的時(shí)間。感興趣的小伙伴可以試一試2022-01-01Python實(shí)現(xiàn)分割文件及合并文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)分割文件及合并文件的方法,涉及Python針對(duì)文件的分割與合并操作相關(guān)技巧,通過(guò)自定義函數(shù)split與join實(shí)現(xiàn)了文件的分割與合并操作,需要的朋友可以參考下2015-07-07python實(shí)現(xiàn)數(shù)通設(shè)備端口監(jiān)控示例
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)通設(shè)備端口監(jiān)控示例,需要的朋友可以參考下2014-04-04Python的多種對(duì)象工廠模式方便代碼維護(hù)擴(kuò)展
這篇文章主要為大家介紹了Python的多種對(duì)象工廠模式更方便我們進(jìn)行代碼維護(hù)擴(kuò)展,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01基于windows下pip安裝python模塊時(shí)報(bào)錯(cuò)總結(jié)
今天小編就為大家分享一篇基于windows下pip安裝python模塊時(shí)報(bào)錯(cuò)總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Python提取PDF中的圖片的實(shí)現(xiàn)示例
本文主要介紹了Python提取PDF中的圖片的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07