周杰伦歌词数据分析python
时间: 2023-06-21 14:08:20 浏览: 71
好的,下面是一个简单的周杰伦歌词数据分析的Python代码示例:
```python
import requests
from bs4 import BeautifulSoup
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
# 获取歌词
url = 'https://www.kugou.com/song/#hash=0E4E18A9F6B7A0CE8C0FE7F3D1D0590A&album_id=0'
response = requests.get(url=url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
lyrics = soup.find('div', {'class': 'text-box'}).text
# 分词
words = jieba.cut(lyrics)
# 统计词频
word_dict = {}
for word in words:
if word not in word_dict:
word_dict[word] = 1
else:
word_dict[word] += 1
# 生成词云
wc = WordCloud(background_color='white', width=800, height=600).generate_from_frequencies(word_dict)
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()
```
说明:
1. 首先通过requests库获取歌词页面的HTML代码,并使用BeautifulSoup库解析出歌词文本。
2. 使用jieba库进行中文分词。
3. 统计每个词出现的次数,生成一个词频字典。
4. 使用wordcloud库生成词云图并显示。
这只是一个简单的示例,如果要对数据进行更深入的分析,可以使用pandas库将数据存储到DataFrame中,并使用matplotlib和seaborn等库进行可视化分析。