写一段爬取网易云音乐入驻歌手热门歌曲评论数的代码
时间: 2023-11-28 21:05:15 浏览: 81
好的,以下是Python爬虫代码:
```python
import requests
import json
from bs4 import BeautifulSoup
# 热门歌曲评论数的URL
url = 'https://music.163.com/weapi/v1/resource/comments/R_SO_4_{song_id}?csrf_token='
# 入驻歌手的歌曲ID
song_ids = ['xxx', 'xxx', 'xxx', ...]
# 请求头
headers = {
'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'}
# 加密参数
def get_params(song_id, page):
data = {
'params': 'X0Z6zYpFtXK3LW/rw0YUcHM3x+qy7OZbQO4V9uJl0Wz0HegL8J9Zd7sdCtTJlPUlMxZwKjN8h9B1FgDxWl6bK+gk2Xo4lYB6PzXJqKn9d0=',
'encSecKey': '2155f8341e7d5d1e1f6cd2ac39aa8ae1a34c1ea4409f3b17c3119a0d52e7b7d2b4b1fa8e67c6d7b4a9a0c0d1f3f6c9e19a2e87dcd29a552ba0a0b0a0e3b7edc6a3d6160d7c8fa3e5a4b3c2c6a0a3a8d2c2e5e8d3b9c0d7d3a1c6c0cdcbc7d3d9c7a1d1d98b8d9b4d6'
}
data['params'] = data['params'].replace('{song_id}', song_id).replace('{page}', str(page))
return data
# 获取评论数
def get_comment_count(song_id):
page = 1
total_comment_count = 0
while True:
response = requests.post(url.format(song_id=song_id), headers=headers, data=get_params(song_id, page))
json_data = json.loads(response.text)
comment_count = len(json_data['comments'])
total_comment_count += comment_count
if comment_count < 20:
break
page += 1
return total_comment_count
# 获取热门歌曲评论数
for song_id in song_ids:
comment_count = get_comment_count(song_id)
print('歌曲ID: {},评论数: {}'.format(song_id, comment_count))
```
代码中使用了requests库发送POST请求,获取加密的json数据。然后用json库解析数据,获取评论数。最后循环获取每首歌曲的评论数并输出。
阅读全文