python爬取网易云评论_Python - 网易云热门评论爬取
时间: 2023-10-30 08:08:53 浏览: 96
下面是一个简单的Python爬取网易云音乐评论的示例代码,仅供参考:
``` python
import requests
import json
import time
import random
import pandas as pd
def get_comments(song_id, offset):
url = 'http://music.163.com/api/v1/resource/comments/R_SO_4_{}/?limit=100&offset={}'.format(song_id, offset)
headers = {
'Referer': 'http://music.163.com/song?id={}'.format(song_id),
'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)
data = json.loads(res.text)
comments = data['comments']
return comments
def main(song_id):
comments_list = []
for i in range(1, 21):
comments = get_comments(song_id, i * 100)
for comment in comments:
comment_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(comment['time'] / 1000))
comments_list.append([comment['user']['nickname'], comment_time, comment['content']])
time.sleep(random.randint(2, 5))
df = pd.DataFrame(comments_list, columns=['user', 'time', 'content'])
df.to_csv('comments.csv', index=False)
if __name__ == '__main__':
main('歌曲ID')
```
需要将代码中的`歌曲ID`替换为实际需要爬取的歌曲ID,运行后会在当前目录下生成一个名为`comments.csv`的文件,包含了歌曲的评论用户、评论时间和评论内容。需要注意的是,为了防止被封IP,这里加入了随机的睡眠时间。
阅读全文