用微信好友个性签名制作词云

前些天上网查资料的时候,我了解到了一个叫做itchat的库,是python当中专门玩微信的第三方库。我想利用这个库,在我的微信里面抓取好友的个性签名,并做成词云。因为我们没有学过itchat库的使用,所以我在制作过程中参考了以下博文:

https://www.cnblogs.com/jiaoyu121/p/6944398.html (主要学习itchat库的用法)

http://www.360doc.com/content/19/1202/10/63953942_876861884.shtml (学习词云效果的展示和调试)

http://www.360doc.com/content/19/1202/10/63953942_876861884.shtml   (scipy库引用出现问题,寻找替代方案)

https://blog.csdn.net/lcj200813138020/article/details/81234631  (怎样利用爬虫抓取个性签名)

https://www.runoob.com/regexp/regexp-syntax.html (如何过滤个性签名当中的emoji等其他字段)

 

 先用python登录网页版微信

 

 

 

 

 

运行后会出现一个二维码

 

 

 

 扫描登陆后,就可以对微信进行操作啦

 获取好友列表和个性签名:

friends = itchat.get_friends(update=True)[0:]
for i in friends:
    signature = i["Signature"]
 
我发现个性签名里面有一些非汉字和英文的元素,如表情符号等。为了处理这样的部分,我参考了博文https://www.runoob.com/regexp/regexp-syntax.html
 
 我照葫芦画瓢写出了这个东西:
signature = i["Signature"].replace(" ", "").replace("span", "").replace("class", "").replace("emoji", "") rep = re.compile("1f\d.+") signature = rep.sub("", signature)
然鹅我其实还不懂为啥这么写,以后我会继续学习的^(* ̄(oo) ̄)^

为了生成词云后能自动弹出预览效果,我们引入plt和imagecolor
刚开始在处理图形的时候,我按照mooc上的方法尝试使用scipy库的imread,然而程序一直报错

 

调试无果后,我上网查找,发现这个scipy库的imread功能已经被停用了

 


 所以我选择了imageio库作为替代

 

彩色的词云看起来过于鲜艳,所以我引入Image库,使字体颜色与背景图贴合,更加清爽

(image库的用法详见https://blog.csdn.net/weixin_42160653/article/details/80234856)

 

 

效果图:

  代码如下

# coding:utf-8
import itchat
import re

itchat.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("1f\d.+")
    signature = rep.sub("", signature)
    tList.append(signature)

text = "".join(tList)

import jieba
wordlist_jieba = jieba.cut(text, cut_all=True)
wl_space_split = " ".join(wordlist_jieba)

import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import PIL.Image as Image import imageio


alice_coloring = imageio.imread( "wechat.jpg") 
my_wordcloud
= WordCloud(background_color="white", max_words=2000, mask=alice_coloring, max_font_size=40, random_state=42,width=2000,height=1000, font_path='汉仪瘦金书简.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()

 

posted @ 2020-04-11 11:34  VVVivian  阅读(291)  评论(0编辑  收藏  举报