python的itchat獲取微信好友數量和性別統計
阿新 • • 發佈:2018-11-19
python獲取微信好友數量和性別統計
先安裝itchat庫
pip install itchat
原始碼如下:
程式碼執行時會跳出二維碼介面,通過手機微信掃碼進行登入
import itchat # 登入微信 # itchat.login() itchat.auto_login(hotReload=True) # 獲取好友列表 friends=itchat.get_friends()[0:] # 男性 male=0 # 女性 female=0 # 未知性別 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("微信好友總數:%d"%(total)) print("男性好友:%.2f%%"%(float(male)/total*100)) print("女性好友:%.2f%%"%(float(female)/total*100)) print("未知性別好友:%.2f%%"%(float(other)/total*100))