python中的itchat子產品可以和微信進行互動。
現在 pycharm 中安裝 itchat 子產品
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL5QDOzETMyYTM0IDNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
使用:
import itchat
import time
import random
itchat.auto_login() #這個子產品可以生成一個二維碼,供我們登入微信
#自動給檔案傳輸助手發送消息
# while True:
# itchat.send('hello westos',toUserName='filehelper')
# time.sleep(random.randint(1,3))
friends = itchat.get_friends()
# print(friends)
info = {}
#計算微信好友清單中的性别統計。
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male',0) + 1
elif friend['Sex'] == 2:
info['female'] = info.get('female',0) + 1
else:
info['other'] = info.get('other',0) + 1
print(info)