# 导入itchat模块import itchat
# 导入pyplot子库import matplotlib.pyplot as plt
# 定义parse_friend循环,提取男女信息数据并存入text字典defparse_friens():
itchat.login()
text =dict()
friends = itchat.get_friends(update=True)[0:]print(friends)
male ="male"
female ="female"
other ="other"#遍历获取男女人数保存到textfor i in friends[1:]:
sex = i['Sex']if sex ==1:
text[male]= text.get(male,0)+1elif sex ==2:
text[female]= text.get(female,0)+1else:
text[other]= text.get(other,0)+1# 好友总人数
total =len(friends[1:])print("A total of "+str(total)+" friends of your WeChat friends")print("Male friends: %.2f%%"%(float(text[male])/ total*100)+"\n"+"Female friends: %.2f%%"%(float(text[female])/ total *100)+"\n"+"Unidentified sex: %.2f%%"%(float(text[other])/ total *100))# 通过draw()函数汇出柱状图
draw(text)# 定义绘出好友柱状图函数defdraw(datas):for key in datas.keys():
plt.bar(key,datas[key])
plt.legend()
plt.xlabel('sex')
plt.ylabel('rate')
plt.title("Gender of Alfred's friends")
plt.show()
parse_friens()