大前天我们通过Python网络爬虫对朋友圈的数据进行了抓取,感兴趣的朋友可以点击进行查看,如何利用Python网络爬虫抓取微信朋友圈的动态(上)和如何利用Python网络爬虫爬取微信朋友圈动态——附代码(下)。今天小编带大家通过词云去将其进行可视化,具体的教程如下。
1、在Python中做词云,需要用到wordcloud库和jieba分词库,没有安装的伙伴可以直接pip安装即可。
![](https://upload-images.jianshu.io/upload_images/9337488-f4b2e735de1ffa15.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
2、之后你可能还需要一些字体,如simhei.ttf等,这些字体在网上都有,可以直接进行下载,在做词云的时候会用得到,如下图所示。
![](https://upload-images.jianshu.io/upload_images/9337488-2c2ca3495cfd3843.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/629)
3、在items.py的同级目录下建立analyse.py文件,定义analyse_words方法,用于实现词云可视化,jieba.cut用于得到分词结果,具体的代码实现,如下图所示。因为得到的moment.json数据是以JSON格式存储的,所以需要在该文件中导入JSON模块对其进行解析。
![](https://upload-images.jianshu.io/upload_images/9337488-a1e8e766bb618403.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
这个地方需要注意一下,由于我们的memoent.json文件中是中文字符,如果在open()函数中没有加入encoding=’utf-8’的话会导致gbk编码错误,记得将编码加上即可。
4、之后运行程序,得到keys.png图片文件,程序运行的效果如下图所示。可以看到keys.png已经在items.py目录下了。
![](https://upload-images.jianshu.io/upload_images/9337488-603eaadea3fa4648.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
5、双击keys.png,如下图所示。
![](https://upload-images.jianshu.io/upload_images/9337488-0e741a79811a5b57.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
6、不得不承认,这个词云图片内容确实丰富,不过也十分的丑。小编利用wordart(一个词云网站)将朋友圈数据进行更加美化的可视化。
![](https://upload-images.jianshu.io/upload_images/9337488-d1171f951bf9858e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
7、比方说用动物的图案进行可视化,效果图如下图所示。
![](https://upload-images.jianshu.io/upload_images/9337488-a28899d93af47aae.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
8、如果直接将数据进行导入的话,wordart会直接将整段话进行可视化,这样显得十分冗余,看上去也不太友好,因此还需要通过Python对数据进行分频统计,之后再导入到wordart中就可以看到想要的效果了。首先,我们需要对json文件进行处理一下,将文本全部导出来,形成一个moment.txt文件。内容如下图所示。
![](https://upload-images.jianshu.io/upload_images/9337488-135bd42dd84670e3.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
9、编写代码,将文本进行分词,代码实现如下所示。
![](https://upload-images.jianshu.io/upload_images/9337488-aee65524c79c2763.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
10、程序运行完成之后,得到的moment_outputs.txt文件,内容如下图所示,可以很清楚的看到分词情况。红色部分是程序运行的过程。
![](https://upload-images.jianshu.io/upload_images/9337488-730c4961a690d001.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
11、继续编写代码,将词频进行统计汇总,代码实现如下图所示。
![](https://upload-images.jianshu.io/upload_images/9337488-8a9bbc0a0f76dddc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
12、程序运行之后,得到一个txt和Excel文件,里边是关于词频统计的信息,如下图所示。红色部分是程序运行的结果,并没有报错。
![](https://upload-images.jianshu.io/upload_images/9337488-9387f629a116d439.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
13、将这些关键字导入到wordart中进行可视化,如下图所示。
![](https://upload-images.jianshu.io/upload_images/9337488-59d6d572718fbf8c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/578)
14、设置一下图案、字体、排版、颜色等等,就可以生成绚丽的词云图了,下图是汪星人词云图。
![](https://upload-images.jianshu.io/upload_images/9337488-246162cf1895cd9d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
15、下图是小云朵词云图,可以给视觉带来一场饕餮盛宴。
![](https://upload-images.jianshu.io/upload_images/9337488-e8bcdac692e23b1e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
小伙伴们,你们有没有觉得很神奇呢?喜欢的话欢迎收藏和转载噢~~