Python实践-itchat获取微信好友总人数以及男女比例

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/King_key/article/details/79249886

1 itchat是微信的个人开放接口;本文利用Python+itchat实现简单的个人好友数量以及男女比例的统计

  itchat 的安装:pip install itchat(需要权限的可以使用sudo)

2 实验代码以及结果截图

#coding=utf-8
import itchat,time
itchat.login()
#微信好友男女比例分析
friends=itchat.get_friends(update=True)[0:]

male=female=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("男性好友: %.2f%%"%(float(male)/total*100)+"\n"
    + "女性好友: %.2f%%"%(float(female)/total*100)+"\n"
    + "性别不明:  %.2f%%"%(float(other)/total*100))

print "好友数量: ",male+female+other
print "男性好友: ",male
print "女性好友: ",female
print "性别不明: ",other

#饼状图输出【这部分为后加,所以数据与前面不相符】

import matplotlib.pyplot as plt

labels="male","female","other"
sizes=[(float(male)/total*100),(float(female)/total*100),(float(other)/total*100)]
colors=['yellowgreen','gold','lightskyblue']
explode=(0,0.1,0)

plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.1f%%',shadow=True,startangle=90)
plt.axis('equal')
plt.show()

3  女性朋友稍多一点也就不说什么了,可是这性别不明是什么个意思?小编也是很无奈呀鄙视

扫描二维码关注公众号,回复: 3535024 查看本文章

[项目Github地址](https://github.com/King-Key/Test/blob/itchat/itchat/itchat-0.py)

猜你喜欢

转载自blog.csdn.net/King_key/article/details/79249886