python获取微信好友数量和性别统计
先安装itchat库
pip install itchat
源代码如下:
代码执行时会跳出二维码界面,通过手机微信扫码进行登录
import itchat
# 登录微信
# itchat.login()
itchat.auto_login(hotReload=True)
# 获取好友列表
friends=itchat.get_friends()[0:]
# 男性
male=0
# 女性
female=0
# 未知性别
other=0
for i in friends[1:]:
sex=i['Sex']
if sex==1:
male+=1
elif sex==2:
female+=1
else:
other+=1
# 微信好友数量
total=len(friends[1:])
print("微信好友总数:%d"%(total))
print("男性好友:%.2f%%"%(float(male)/total*100))
print("女性好友:%.2f%%"%(float(female)/total*100))
print("未知性别好友:%.2f%%"%(float(other)/total*100))