#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的公众号,下面是示例程序ÿ