需要准备的库:
request----------------爬虫包
re-----------------------正则包
wordcloud------------词云
matplotlib.pyplot----处理生成图片
jieba-------------------处理词云中文乱码
话不多说直接上成品图
源码在此(原理简单,在此不做过多赘述):
import requests
import re
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
Referer = "https://movie.douban.com/subject/35096844/comments?status=P"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54"
}
respone = requests.get(headers=headers, url=Referer).text
reg = r"""<span class="short">(.*?)</span>"""
reg = re.findall(reg, respone)
with open('ttt.txt', 'w') as f:
for i in reg:
f.write(i)
f.write('\n')
def read_fun():
with open("ttt.txt", "r") as f:
txt = f.read()
re_move = [",", "。", "\n", "\xa0"]
for i in re_move:
txt = txt.replace(i, " ")
word = jieba.lcut(txt)
with open("txt_save.txt", "w") as file:
for i in word:
file.write(str(i) + " ")
print('文本处理完成')
def imag_fun():
with open("txt_save.txt", "r") as fp:
txt = fp.read()
WCloud = WordCloud(
width=800,
height=800,
font_path="simhei.ttf").generate(txt)
WCloud.to_file("test.png")
plt.imshow(WCloud)
plt.axis('off')
plt.show()
if __name__ == '__main__':
read_fun()
imag_fun()