ps:网上的demo自己练习的。
# coding:utf-8
import itchat
import re
itchat.login() # 会形成一个二维码 用于登陆微信
friends = itchat.get_friends(update=True)[0:] # 好友基本信息,friend[0]是自己
# print(friends) #所有好友信息
tList = []
for i in friends:
# 好友信息循环处理
signature = i["Signature"].replace(" ", "").replace("span", "").replace("class", "").replace("emoji", "")# 过滤个性签名中的表情和特殊字符
rep = re.compile("1f\d.+") # 正则处理数据 正则匹配过滤掉emoji表情
signature = rep.sub("", signature) # signature个性签名
# print(signature) #所有好友信息的个性签名
tList.append(signature) # append 将传入的对象附加(添加)到现有列表中。
# 拼接字符串
text = "".join(tList)
# print(text)
# jieba分词
import jieba
wordlist_jieba = jieba.cut(text, cut_all=True)
wl_space_split = " ".join(wordlist_jieba)
# wordcloud词云
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import os
import numpy as np
import PIL.Image as Image
from PIL import Image
d= os.path.dirname(os.path.abspath( __file__ ))
d = os.path.join(d, "wechat.jpg")
d = d.replace("\\","/")
alice_coloring = np.array(Image.open(d)) # 处理路径
# 画布设置
my_wordcloud = WordCloud(background_color="white", max_words=2000,mask=alice_coloring,max_font_size=400, random_state=420,font_path='C:/Windows/Fonts/simhei.ttf').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()
网友评论