python爬虫之:抓取网易云音乐某首歌曲的热门评论
首先,以这首歌为例:

python 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import requests import json # import BeautifulSoup from bs4 def get_hotComments(res): comments_json = json.loads(res.text) hot_comments = comments_json['hotComments'] with open('/Users/yao/www/python/evol-love.txt', 'w', encoding='utf-8') as file: for each in hot_comments: file.write(each['user']['nickname'] + ': \n\n') file.write(each['content'] + '\n') # file.write(each['likedCount'] + '\n') # file.write(each['time'] + '\n') file.write('--❤-----------❤--\n') def get_comments(url): song_id = url.split('=')[1] headers = { 'user-agent': 'curl https://bootstrap.pypa.io/get-pip.py | python3', 'referer': 'https://music.163.com/song?id=1381290746'} params = '15S1vG2hDWPcsAeXpjYIcwuRpK7nwElk3Cdy6c/3aJdbH5aBub7nWrYPxZQaMzKrhFSUo1n4nGHkCbaZAl77WSFPMYjjv6sU5Qipnqnn4QjXRH6eN1w/PcauCCUo4NDgaw/Uudad5J/zpgDY2005gC3UF+s2s/ND5tjXTU0SeDkiBg47A6b6VqhK/7E/hS9++nlvAC7QkkzCbsXZMp/jyBl5+KroeMcGAfFumxZ5pME=' encSecKey = '3f5dbdd0ec1f0a1501b88f6e04ce30ecf88192b4e3a92bd2dc1216f2ccc90898211e6347a2ab36d07f93793352ff6559fb927b7cbd337f5887d2af7b442e2dfb0b4abd83e85c6c3bb20fe2beef050db4c169531411f4e23caaa201f62609fcda4de694c8e9e759443fea2f7febf044bd9c4bcb2697519ca96dca676730059156' data = { 'params': params, 'encSecKey': encSecKey } target_url = 'https://music.163.com/weapi/v1/resource/comments/R_SO_4_{}?csrf_token=55a6b8514346a9f676c8485978093b9f'.format(song_id) res = requests.post(target_url, headers=headers, data=data) # rawHTML = '' # soup = BeautifulSoup(rawHTML) # file_name_obj = soup.select_one('.f-ff2') # file_name = file_name_obj.text return res def main(): url = input('please input song url:') res = get_comments(url) # title = get_title(url) get_hotComments(res) if __name__ == "__main__": main()
|
得到的txt文件如下(如有需要可进一步分析):
