1. 程式人生 > >python分析統計自己微信朋友的資訊

python分析統計自己微信朋友的資訊

首先,你得安裝itchat,命令為pip install itchat,其餘的較為簡單,我不再說明,直接看註釋吧。

以下的程式碼我在Win7+Python3.7裡面除錯通過
__author__ = 'Yue Qingxuan'
# -*- coding: utf-8 -*-
import itchat

# hotReload=True可不用每次都去掃描二維碼,只需要手機上確認下
itchat.auto_login(hotReload=True)
# 獲取好友列表
friends = itchat.get_friends(update=True)[0:]

# 初始化計數器,有男有女,當然,有些人是不填的
male = female = other = 0

# 遍歷這個列表,列表裡第一位是自己,所以從"自己"之後開始計算,其中sex=1時表示男性,2為女性,0是未註明性別的
for i in friends[1:]:
sex = i["Sex"]
if sex == 1:
male += 1
elif sex == 2:
female += 1
else:
other += 1
#這裡可以輸出哪些是未註明性別的
print("NickName=",i['NickName'],"\t\t RemarName",i['RemarkName'])

# 算上微信朋友總數,計算比例
total = len(friends[1:])

# 好了,列印結果
print("微信朋友數量=",str(len(friends[1:])))
print("男性好友數量%d,佔比:%.2f%%" % (male,float(male) / total * 100))
print("女性好友數量%d,佔比:%.2f%%" % (female,float(female) / total * 100))
print("未明性別數量%d,佔比:%.2f%%" % (other,float(other) / total * 100))