需要安装itchat、matplotlib、numpy、jieba等依赖。
注意事项:
为了使matplotlib支持中文,需要指定中文字体,本文设置的是ubuntu下的字体文件路径,在使用时需要换成当前环境下的字体路径。
效果如下:
看来还是男性最多
在北京待了这么长时间,当然是北京的好友多了,另外老家是湖北的,所以其次湖北多
废话不多说,代码如下,都是抄袭参考资料里的,请见谅:
#coding: utf-8 import itchat import re import jieba import matplotlib as mpl from matplotlib import pyplot as plt import numpy as np zhfont = mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf') def friend_analysis(friends): labels = ['male', 'female', 'other'] data = [0, 0, 0] # friends[0] 是自己的信息 for friend in friends[1:]: sex = friend["Sex"] if sex == 1: data[0] += 1 elif sex == 2: data[1] += 1 else: data[2] += 1 fig = plt.figure() plt.pie(data, labels=labels, autopct="%.2f%%") plt.title("friend analysis") plt.show() def province_analysis(friends): province_map = dict() for friend in friends[1:]: province = friend["Province"] province_map[province] = province_map.get(province, 0) + 1 province_list = sorted(province_map.items(), key=lambda item:item[1], reverse=True)[:15] [provinces, heights] = zip(*province_list) lefts = np.arange(len(provinces)) + 0.5 fig, ax = plt.subplots() bars = ax.bar(lefts, heights, 0.35) ax.set_ylabel('count') ax.set_title('province analysis') ax.set_xticks(lefts) ax.set_xticklabels(provinces, fontproperties=zhfont) for bar in bars: height = bar.get_height() ax.text(bar.get_x(), height + 3, '%d' % int(height)) plt.show() def signature_analysis(friends): signature_list = list() for friend in friends[1:]: signature = friend['Signature'].strip() signature_list.append(signature) print(friend['NickName']+"\t"+signature) if __name__ == "__main__": itchat.auto_login() friends = itchat.get_friends(update=True) friend_analysis(friends) province_analysis(friends) signature_analysis(friends)
参考:
1. itchat github地址
https://github.com/littlecodersh/ItChat
2. itchat 作者博客地址
http://itchat.readthedocs.io/zh/latest/
3. 其他参考
https://mp.weixin.qq.com/s/FQXsBLlukmAln1l6EYVftw
http://beanxyz.blog.51cto.com/5570417/1909865
http://blog.sina.com.cn/s/blog_8c5e4fc30102x3eq.html