import itchat
import re
from echarts import Echart, Legend, Pie
def friend_date():
# 登录
itchat.login()
# 获取好友列表
friends = itchat.get_friends(update=True)[0:]
male = female = 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('男性朋友占比为:%.2f%%' % (float(male) / total * 100))
print('女性朋友占比为:%.2f%%' % (float(female) / total * 100))
print('其他朋友占比为:%.2f%%' % (float(other) / total * 100))
chart = Echart('%s的微信好友性别比例' % (friends[0]['NickName']), 'from WeChat')
chart.use(Pie('WeChat',
[{'value': male, 'name': '男性%.2f%%' % (float(male) / total * 100)},
{'value': female, 'name': '女性%.2f%%' % (float(female) / total * 100)},
{'value': other, 'name': '其他%.2f%%' % (float(other) / total * 100)}],
radius=['50%', '70%']))
chart.use(Legend(['male', 'female', 'other']))
del chart.json['xAxis']
del chart.json['yAxis']
chart.plot()
def signature_cloud():
# 登录
itchat.login()
# 获取好友列表
friends = itchat.get_friends(update=True)[0:]
tList = []
for i in friends:
signature = str(i['Signature']).strip().replace(' ', '').replace('span', '').replace('class', '').replace(
'emoji', '')
# 正则过滤emoji表情等字符
rep = pile('lf\d.+')
signature = rep.sub('', signature)
if signature != '':
tList.append(signature)
# 拼接字符串
text = "".join(tList)
# jieba分词
import jieba
wordlist_jieba = jieba.cut(text, cut_all=True)
wl_space_split = ' '.join(wordlist_jieba)
# word cloud词云
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import os
import numpy as np
import PIL.Image as Image
d = os.path.dirname(__file__)
alice_coloring = np.array(Image.open(os.path.join(d, 'mask\\0403024005894.jpg')))
my_wordcloud = WordCloud(background_color='white', max_words=500,
max_font_size=80, random_state=42,
font_path=r'E:\python\data_mining\com\lubinsu\wechat\msyhbd.ttc',
mask=alice_coloring).generate(wl_space_split)
image_colors = ImageColorGenerator(alice_coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis('off')
plt.show()
my_wordcloud.to_file(os.path.join(d, 'wechat_cloud.png'))
itchat.send_image('wechat_cloud.png', 'filehelper')
if __name__ == '__main__':
signature_cloud()