寒暄的话不多说,直接进入主题。

运行效果图:

python在列表分别统计男女人数 python统计文件中男女人数_微信

python在列表分别统计男女人数 python统计文件中男女人数_python中如何获得男生人数_02

 

 

【准备环境】

  Python版本:v3.5及其以上

  开发工具:随意,此处使用Pycharm

 

【依赖包】

1、itchat (CMD运行:pip install itchat   进行安装)

2、pycharts (CMD运行:pip install pyecharts   进行安装)

 

itchat包是对网页版微信相关接口封装的一个第三方包,目前来说比较好用,一会代码里面会用到相关接口(注释说明);

  pycharts包进行图表的创建,只是用到了其初级功能,大家有时间可以将代码改改,生成更全面直观的图表,代码中使用的柱状图,稍显low,可以升级为全国热点图,这样人员的地区分布就更加直观了。

 

代码:

 

1 import itchat
  2 from collections import Counter
  3 from pyecharts import Bar
  4 
  5 dict_sex = {}
  6 count_city = None
  7 
  8 # itchat微信登录,hotReload表示热登录,如果是True,下一次就不用扫码了(时间不能过长),会在根目录生成一个 itchat.pkl 的文件
  9 itchat.auto_login(hotReload=True)
 10 # itchat 的get_friends接口,获取微信好友列表,返回的列表第一位是你自己,如果想过滤掉自己,改为:itchat.get_friends()[1:]
 11 member_list = itchat.get_friends()[0:]
 12 
 13 
 14 def calc_all_sex():
 15     """
 16     微信联系人总男女信息
 17     :return:
 18     """
 19     man = woman = others = 0
 20     city = []
 21     for index, name in enumerate(member_list):
 22         print("\t{}、{}({})".format(index, name["RemarkName"] if name["RemarkName"] is not "" else name["NickName"], name["UserName"]))
 23         sex = name["Sex"]
 24         if sex == 1:
 25             man += 1
 26         elif sex == 2:
 27             woman += 1
 28         else:
 29             others += 1
 30         if name["City"] == "":
 31             city.append("未知城市")
 32         else:
 33             city.append(name["City"])
 34 
 35     global count_city
 36     count_city = Counter(city)
 37     total = len(member_list)
 38     man_percent = (float(man) / total * 100)
 39     woman_percent = (float(woman) / total * 100)
 40     others_percent = (float(others) / total * 100)
 41 
 42     print("\n>>>>>>>>>>>>>微信联系人总男女信息:")
 43     print("男性好友:%.2f%%" % man_percent)
 44     print("女性好友:%.2f%%" % woman_percent)
 45     print("其    它:%.2f%%" % others_percent)
 46 
 47 
 48 class PeopleInfo:
 49     def __init__(self, man_, woman_, _others, total_):
 50         self.man = man_
 51         self.woman = woman_
 52         self.others = _others
 53         self.total = total_
 54 
 55 
 56 def count(dict_={}):
 57     """
 58     计算各个地区的男女人数
 59     :param dict_:
 60     :return:
 61     """
 62     print("\n>>>>>>>>>>>>>各地区男女分布信息:")
 63     for val in dict_:
 64         city_tmp = "" if val == "未知城市" else val
 65         man = woman = others = 0
 66         for member in member_list:
 67             if member["City"] == city_tmp:
 68                 sex = member["Sex"]
 69                 if sex == 1:
 70                     man += 1
 71                 elif sex == 2:
 72                     woman += 1
 73                 else:
 74                     others += 1
 75         people_info = PeopleInfo(man, woman, others, dict_[val])
 76         dict_sex[val] = people_info
 77         print("【{}】男性:{},女性:{},其它:{}".format(city_tmp, man, woman, others))
 78 
 79 
 80 def count_sex_area():
 81     """
 82     统计联系人性别、地区
 83     :return:
 84     """
 85     calc_all_sex()
 86     attr = ["{}".format(i) for i in count_city]
 87     count(count_city)
 88     v1 = []
 89     man_count = []
 90     woman_count = []
 91     others_count = []
 92     for i in attr:
 93         v1.append(count_city[i])
 94         man_count.append(dict_sex[i].man)
 95         woman_count.append(dict_sex[i].woman)
 96         others_count.append(dict_sex[i].others)
 97 
 98     bar = Bar(title="{}的微信联系人分布".format(member_list[0]["NickName"]), subtitle="微信联系人分布情况", width=2024, height=768)
 99     bar.add("地区人数", attr, v1, mark_line=["average"], mark_point=["max", "min"])
100     bar.add("男性", attr, man_count, mark_line=["average"], mark_point=["max", "min"])
101     bar.add("女性", attr, woman_count, mark_line=["average"], mark_point=["max", "min"])
102     bar.render(path="地区统计.html")
103 
104 
105 def get_signatare():
106     """
107     获取微信联系人的签名信息
108     :return:
109     """
110     for member in member_list:
111         signatare = str(member["Signature"])
112         print("\n{}:\n\t>>>>>:{}".format(member["RemarkName"], signatare))
113 
114 
115 if __name__ == '__main__':
116     count_sex_area()
117     # get_signatare()