目标:提取陈奕迅的歌词常用词,并做词语展示
大致过程如图片所示(来自极客时间-数据分析实战)
过程需要三种工具:
1、词云制作工具:
python提供了词云工具WordCloud。词云构造函数:
wc = WordCloud(
background_color='white',# 设置背景颜色
mask=backgroud_Image,# 设置背景图片
font_path='./SimHei.ttf', # 设置字体,针对中文的情况需要设置中文字体,否则显示乱码
max_words=100, # 设置最大的字数
stopwords=STOPWORDS,# 设置停用词
max_font_size=150,# 设置字体最大值
width=2000,# 设置画布的宽度
height=1200,# 设置画布的高度
random_state=30# 设置多少种随机状态,即多少种颜色
)
生成词云:
wordcloud=generate(text)
使用wordcloud.tofile(“a.jpg”)进行保存;
或用Python可视化工具Matplotlib进行显示:
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
注:在用jieba对文本进行分词后,要去掉常用词,调用remove_stop_words函数:
def remove_stop_words(f):
stop_words = ['学会', '就是', '什么']
for stop_word in stop_words:
f = f.replace(stop_word, '')
return f
2、python爬虫方法,爬取歌曲信息
3、XPath工具的使用,分析提取元素
用python爬取HTML,再使用XPATH对歌曲的ID、名称进行解析,然后通过网易云音乐的API接口获取歌曲的歌词,最后将歌词合并成一个文本。
代码如下: