爬取数据分析——将豆瓣电影top250以词云的方式展现

根据爬取到的豆瓣top250电影信息,根据一句话概述,首先使用jieba分词工具进行分词,再使用wordcloud进行词云展示

# -*- codeing = utf-8 -*-
# @Time : 2020/7/14 0:11
# @Author: 小菜菜最菜
# @File : testCloud.py
# @Software : PyCharm

import jieba
from matplotlib import pyplot as plt
from wordcloud import WordCloud
from PIL import Image   # 图片处理
import numpy as np      # 矩阵运算
import sqlite3

# 这里已将数据存储在movie.db中,存储方法可见前面博客,使用的数据库为sqlite3
conn = sqlite3.connect('movie.db')
cur = conn.cursor()
sql = 'select instroduction from movie250'
data = cur.execute(sql)
text = ""
for item in data:
    text = text + item[0]
cur.close()
conn.close()
print(text)
print('-----------------')


# 分词
cut = jieba.cut(text)
string = ' '.join(cut)
print(len(string))


# 画图
img = Image.open(r'./static/assets/img/tree.jpg')
img_array = np.array(img)# 图片转化为数组
wc = WordCloud(
    background_color='white',
    mask = img_array,
    font_path = "msyh.ttc"
)
wc.generate_from_text(string)



#绘制图片
fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off')   # 是否显示坐标轴
# plt.show()    # 显示生成的词云图片


# 输出词云图片到文件
plt.savefig(r'.\static\assets\img\word.jpg',dpi=500)

生成的词云图片为:

posted @ 2020-07-14 22:04  小菜菜最菜  阅读(753)  评论(0编辑  收藏  举报