使用wxpy统计微信好友数量及男女数量,并保存好友数据到本地

本文介绍了一种使用wxpy库统计微信好友数量及性别比例的方法,并将数据保存为JSON文件。通过Python编程,实现了好友信息的抓取,包括微信ID、城市、性别、昵称等,并进行了性别比例统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值