【Python】【Matplotlib】词云图
关于从网页获取文本
import requests
from bs4 import BeautifulSoup
code = requests.request("post","url").content.decode("utf-8")
soup = BeautifulSoup(code, "lxml")
text = soup.findAll("div",attrs={"class":"RichText"})[0].text
text = str(text).replace("\u3000"," ")
text
关于绘制词云图
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 正常显示负号
# 分词
words = ' '.join(jieba.cut(text))
# 创建词云
wordcloud = WordCloud(font_path="AlimamaFangYuanTiVF-Thin.ttf",width=1000,height=700,background_color="pink").generate(words)
# 显示词云
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
AlimamaFangYuanTiVF-Thin.ttf
是字体文件,和该代码文件是放在同一目录的。你可以选择自己喜欢的字体。
版 权 声 明
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步