通过抓取网易云音乐的评论内容,进行词云分析来研究用户对音乐的看法,抓取的音乐是赵雷的成都,费玉清的一剪梅,具体代码怎么写
时间: 2024-04-02 20:35:52 浏览: 28
这个任务可以通过 Python 的第三方库 requests 和 BeautifulSoup 实现。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
from wordcloud import WordCloud
# 抓取评论数据
def get_comments(song_id):
url = f"https://music.163.com/playlist?id={song_id}"
headers = {
"Referer": "https://music.163.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
comments = []
for comment in soup.select(".m-cmmt"):
comments.append(comment.select_one(".cnt").text.strip())
return comments
# 生成词云图
def generate_wordcloud(text):
wordcloud = WordCloud(width=800, height=800, background_color="white", collocations=False).generate(text)
wordcloud.to_file("wordcloud.png")
# 抓取评论并生成词云图
song_id = "66842" # 成都
comments = get_comments(song_id)
text = " ".join(comments)
generate_wordcloud(text)
```
你需要替换 `song_id` 的值为你要抓取的歌曲ID,例如赵雷的成都的ID是66842,费玉清的一剪梅的ID是28427767。此外,你需要安装以下第三方库:requests, BeautifulSoup, wordcloud,可以使用pip命令进行安装。
相关推荐
![.rar](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![png](https://img-home.csdnimg.cn/images/20210720083516.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)