Loading


今天学习了使用Scrapy框架

import collections
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba #分词用
from wordcloud import WordCloud, STOPWORDS
path_txt = 'E:/data.txt'
#从文件中读
file = open(path_txt, encoding="utf-8").read()
#进行分词
default_mode =jieba.cut(file)
text = " ".join(default_mode)
#自定义去除词库
remove_words = [u'的', u',',u'和', u'并', u'前端', u'web', u'开发',u'熟悉',u'了解',u'都',u'。',u'',u'、',u';',u'.',u'中',u'在',u'了',u'需要',
                u'掌握',u'1',u'2',u'3',u'4',u'5',u'6',u'-',u'/',u'有',u'等',u'对',u'及',u'年',u';',u'(',u')',u',',u'(',u')',u':',u'\n',u'\xa0',
                u'能',u'或',u'者'] 
# 词频统计
object_list = []
for word in text.split(' '):
    if word not in remove_words:
        object_list.append(word)
word_counts = collections.Counter(object_list) # 对分词做词频统计
word_counts_top50 = word_counts.most_common(50) # 获取前50最高频的词
print (word_counts_top50) #输出检查
#存入数据库
# conn = pymysql.connect(**config)
# c = conn.cursor()
# sql = 'insert into wordcloud(word,value) values(%s,%s)'
# for i in word_counts_top50:
#     c.execute(sql, (i[0],str(i[1])))
# conn.commit()
# conn.close()

alice_mask = np.array(Image.open( r"E:/3.png"))
print('加载图片成功!')
wc = WordCloud(
    #设置字体,不指定就会出现乱码,这个字体文件需要下载
    font_path=r'E:/wqy-microhei.ttc',
    background_color="white",
    max_words=2000,
    mask=alice_mask,
    stopwords=remove_words)
# 生成词云
wc.generate(text)
print('开始加载文本!')
# 存入文件
wc.to_file(r"E:/result.jpg")
print('生成词云成功!')
# 展示
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.show()

  今天出现了错误,,明天计划把词云做出来

posted on 2020-02-15 23:09  一氓  阅读(139)  评论(0编辑  收藏  举报