使用wxpy统计微信好友数量及男女数量,并保存好友数据到本地,代码如下:
from wxpy import *
import json
class bots:
def __init__(self):
pass
def initBot(self):
bot = Bot()
my_friend = bot.friends();
self.friendsArr=[]
sexArr=['未知','男','女']
for ff in my_friend:
ffDict={}
ffDict.update({'wxid':ff.wxid})
ffDict.update({'city':ff.city})
ffDict.update({'sex':sexArr[ff.sex]})
ffDict.update({'isFriend':ff.is_friend.nick_name})
ffDict.update({'nick_name':ff.nick_name})
ffDict.update({'user_name':ff.user_name})
ffDict.update({'province':ff.province})
ffDict.update({'remark_name':ff.remark_name})
self.friendsArr.append(ffDict)
def setFriendsDataToJson(self,fileName):
f=open(fileName,'w+')
jstr=json.dumps(self.friendsArr)
f.write(jstr)
def getTheSexNum(self):
unknown,male,female=0,0,0
# print(self.friendsArr[0])
# return
for item in self.friendsArr:
if item.get('sex')=='男':
male+=1
elif item.get('sex')=="女":
female+=1
else:
unknown+=1
print('count:'+str(len(self.friendsArr)),'男:'+str(male),'女:'+str(female))
botObj=bots()
botObj.initBot()
botObj.getTheSexNum()