鱼C论坛

 找回密码
 立即注册
查看: 1747|回复: 2

[已解决]关于爬取网易云音乐评论的错误

[复制链接]
发表于 2020-7-26 01:03:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
相对课程中的函数,我改用了普通形式,但总会显示line35  KeyError:’hotComments‘,不论是’hotComments‘还是其他的,就是找不到。是哪里出错了吗
有大神帮帮忙嘛!

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Jul 26 00:18:36 2020

  4. @author: Chang YF
  5. """


  6. import requests
  7. import json


  8. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
  9.            'referer': 'http://music.163.com/'}

  10. params = '3a1OtDUiNmbCVaP1B5CPYSeyY/wDsYmmJwYd2jd3aCYZeUxW1WFFrS0XeT4MZOVXl04cibj3INebigj8HJZ51ZjXarsRz92q1IGzamJtEtEsyHs9e611kL8S/bngl8HQWQMj6e3BT1t412zB0YWZ9QzbH0hHiTienBvLVm4hrH63aTjpBu5BCaxYQcM23QuwRvFC0sQyM5764/zw7gQEKz3zHhg+BH5rCfHVjnK+nc4DwiiEnRTXZ1S4LTLt9xpC8g9MQ2yXWhDEVQ0U4xxuPQ=='

  11. encSecKey = 'b5295ea54521dd92971c8d7ec485ae485cd46e6ad78755856d559a497e82a697764d1659229c31c55a633bbc02f775a24e4df9b8b0019abc119979ffaadfbed6892fcab1e43155acc643e660f98dc67d86c89aaa5be8dbd9d3f4bba62ad25b1ab2342cc66dd09e40f26e23647d0e01c348511332ad6c7e33da504b0f8a4b4f00'

  12. data = {'params':params,'encSecKey':encSecKey}

  13. url = 'https://music.163.com/#/song?id=4466775'

  14. name_id =url.split('=')[1]

  15. target = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='

  16. res = requests.post(target, headers=headers, data=data)

  17. with open('res.txt','w',encoding='utf-8') as file:
  18.     file.write(res.text)
  19.    
  20. comments_json = json.loads(res.text)
  21. print(comments_json)
  22. hot_comments = comments_json["hotComments"]
  23. with open('hot_comments.txt', 'w', encoding='utf-8') as file:
  24.     for each in hot_comments:
  25.         file.write(each['user']['nickname'] + ':\n\n')
  26.         file.write(each['content'] + '\n')
  27.         file.write("---------------------------------------\n")
复制代码
最佳答案
2020-7-26 08:03:49

hotComments 键,在 data 里面,你需要先读取 data ,然后在获取 hotComments

把 hot_comments = comments_json["hotComments"] 改成 hot_comments = comments_json["data"]["hotComments"]即可:

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Jul 26 00:18:36 2020

  4. @author: Chang YF
  5. """

  6. import requests
  7. import json

  8. headers = {
  9.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
  10.     'referer': 'http://music.163.com/'}

  11. params = '3a1OtDUiNmbCVaP1B5CPYSeyY/wDsYmmJwYd2jd3aCYZeUxW1WFFrS0XeT4MZOVXl04cibj3INebigj8HJZ51ZjXarsRz92q1IGzamJtEtEsyHs9e611kL8S/bngl8HQWQMj6e3BT1t412zB0YWZ9QzbH0hHiTienBvLVm4hrH63aTjpBu5BCaxYQcM23QuwRvFC0sQyM5764/zw7gQEKz3zHhg+BH5rCfHVjnK+nc4DwiiEnRTXZ1S4LTLt9xpC8g9MQ2yXWhDEVQ0U4xxuPQ=='

  12. encSecKey = 'b5295ea54521dd92971c8d7ec485ae485cd46e6ad78755856d559a497e82a697764d1659229c31c55a633bbc02f775a24e4df9b8b0019abc119979ffaadfbed6892fcab1e43155acc643e660f98dc67d86c89aaa5be8dbd9d3f4bba62ad25b1ab2342cc66dd09e40f26e23647d0e01c348511332ad6c7e33da504b0f8a4b4f00'

  13. data = {'params': params, 'encSecKey': encSecKey}

  14. url = 'https://music.163.com/#/song?id=4466775'

  15. name_id = url.split('=')[1]

  16. target = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='

  17. res = requests.post(target, headers=headers, data=data)

  18. with open('res.txt', 'w', encoding='utf-8') as file:
  19.     file.write(res.text)

  20. comments_json = json.loads(res.text)
  21. print(comments_json)
  22. hot_comments = comments_json['data']["hotComments"]
  23. print(hot_comments)
  24. with open('hot_comments.txt', 'w', encoding='utf-8') as file:
  25.     for each in hot_comments:
  26.         file.write(each['user']['nickname'] + ':\n\n')
  27.         file.write(each['content'] + '\n')
  28.         file.write("---------------------------------------\n")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-26 08:03:49 | 显示全部楼层    本楼为最佳答案   

hotComments 键,在 data 里面,你需要先读取 data ,然后在获取 hotComments

把 hot_comments = comments_json["hotComments"] 改成 hot_comments = comments_json["data"]["hotComments"]即可:

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Jul 26 00:18:36 2020

  4. @author: Chang YF
  5. """

  6. import requests
  7. import json

  8. headers = {
  9.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
  10.     'referer': 'http://music.163.com/'}

  11. params = '3a1OtDUiNmbCVaP1B5CPYSeyY/wDsYmmJwYd2jd3aCYZeUxW1WFFrS0XeT4MZOVXl04cibj3INebigj8HJZ51ZjXarsRz92q1IGzamJtEtEsyHs9e611kL8S/bngl8HQWQMj6e3BT1t412zB0YWZ9QzbH0hHiTienBvLVm4hrH63aTjpBu5BCaxYQcM23QuwRvFC0sQyM5764/zw7gQEKz3zHhg+BH5rCfHVjnK+nc4DwiiEnRTXZ1S4LTLt9xpC8g9MQ2yXWhDEVQ0U4xxuPQ=='

  12. encSecKey = 'b5295ea54521dd92971c8d7ec485ae485cd46e6ad78755856d559a497e82a697764d1659229c31c55a633bbc02f775a24e4df9b8b0019abc119979ffaadfbed6892fcab1e43155acc643e660f98dc67d86c89aaa5be8dbd9d3f4bba62ad25b1ab2342cc66dd09e40f26e23647d0e01c348511332ad6c7e33da504b0f8a4b4f00'

  13. data = {'params': params, 'encSecKey': encSecKey}

  14. url = 'https://music.163.com/#/song?id=4466775'

  15. name_id = url.split('=')[1]

  16. target = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='

  17. res = requests.post(target, headers=headers, data=data)

  18. with open('res.txt', 'w', encoding='utf-8') as file:
  19.     file.write(res.text)

  20. comments_json = json.loads(res.text)
  21. print(comments_json)
  22. hot_comments = comments_json['data']["hotComments"]
  23. print(hot_comments)
  24. with open('hot_comments.txt', 'w', encoding='utf-8') as file:
  25.     for each in hot_comments:
  26.         file.write(each['user']['nickname'] + ':\n\n')
  27.         file.write(each['content'] + '\n')
  28.         file.write("---------------------------------------\n")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-24 18:08:57 | 显示全部楼层
Twilight6 发表于 2020-7-26 08:03
hotComments 键,在 data 里面,你需要先读取 data ,然后在获取 hotComments

把 hot_comments = com ...

帮我看看这个?
屏幕截图 2020-11-24 132512.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-30 04:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表