玩转Python :获取微信好友个性签名生成词云

运行结果:

玩转Python :获取微信好友个性签名生成词云_第1张图片

需要用到的库itchat、matplotlib、re、wordcloud、jieba。

import itchat
import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud

itchat.auto_login(True)
friends = itchat.get_friends(update=True)[0:]
tList = []
for i in friends:
    signature = i["Signature"].replace(" ", "").replace("span", "").replace("class", "").replace("emoji", "")
    rep = re.compile("< =.+/>")
    signature = rep.sub("", signature)
    tList.append(signature)

# 拼接字符串
text = "".join(tList)

# jieba分词
wordlist_jieba = jieba.cut(text, cut_all=True)
wl_space_split = " ".join(wordlist_jieba)

# #如果是中文必须要添加字体路径,否则会显示成框框
my_wordcloud = WordCloud(background_color="white", width=1600, height=1200,
                         font_path='C:\Windows\Fonts\msyh.ttc').generate(wl_space_split)

plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()

运行过程:

玩转Python :获取微信好友个性签名生成词云_第2张图片

你可能感兴趣的:(学习Python)