利用Python統計微信聯系人男女比例以及簡單的地區分布


寒暄的話不多說,直接進入主題。

運行效果圖:

 

 

【准備環境】

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM