安装itchat包:
[root@westos_netfilesystem day02]# pip3 install itchat
使用此命令安装比较慢:
建议指定由国内的 http://pypi.douban.com/simple安装(此为国内豆瓣提供的镜像源下载itchat包),使用该方法安装非常快:
[root@westos_netfilesystem day02]# pip3 install itchat -i http://pypi.douban.com/simple
# Alt + Insert ==== Create New file
# Ctrl + Alt + S ===== seetings
# python package: itchat
import itchat
# 1. Login weixin
itchat.auto_login(hotReload=True)
# 2. get friends information, return friends List [1, 2, 3, 4, .....]
# Every friend information saved as dict.
friends = itchat.get_friends()
# First friend information is yours
mine = friends[0]
name = mine['NickName']
Signature = mine['Signature']
# init male, female, other = 0
male = female = other = 0
for friend in friends[1:]:
sex = friend['Sex']
if sex == 1:
male += 1
elif sex == 2:
female += 1
else:
other += 1
# Ctrl + D : Quick copy one line
print("""
*******************************weixin INformation***********************
Name: %s
Signature: %s
Male friends count: %d
Female friends count: %d
Unknown sex friends count: %d
""" %(name, Signature, male, female, other))
# 1. Analyse
# friend province
# "shanxi"-200 "beijing"-100 "shandong" 10 .........
# dict: {"province_name":people_num}
# 2.
import itchat
from collections import Counter
# 3.
itchat.auto_login(hotReload=True)
# friends===List, friend===Dict {"NickName":"", "Sex":"", "Province":""}
# First Information is your info
friends = itchat.get_friends()
# First friend information is yours
mine = friends[0]
name = mine['NickName']
Signature = mine['Signature']
# 4.
# Save Province to dict
provinces = {}
for friend in friends[1:]:
province = friend['Province']
if province != '':
# If province not in dict, set value=1
# If province in dict, value += 1
if province in provinces:
# value = provinces[province] # province people count
# value += 1 # value + 1
# provinces[province] = value # set new value
provinces[province] += 1
else:
provinces[province] = 1
# 5.
counter = Counter(provinces)
top_5_provinces = counter.most_common(5)
# 6.
print("""
*******************************weixin INformation***********************
Name: %s
Signature: %s
""" %(name, Signature))
print("*******************************weixin friends Province***********************")
for name, count in top_5_provinces:
print("%s : %s" %(name, count))