鱼C论坛

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

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

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

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

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

x
相对课程中的函数,我改用了普通形式,但总会显示line35  KeyError:’hotComments‘,不论是’hotComments‘还是其他的,就是找不到。是哪里出错了吗
有大神帮帮忙嘛!
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 26 00:18:36 2020

@author: Chang YF
"""


import requests
import json


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',
           'referer': 'http://music.163.com/'}

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

encSecKey = 'b5295ea54521dd92971c8d7ec485ae485cd46e6ad78755856d559a497e82a697764d1659229c31c55a633bbc02f775a24e4df9b8b0019abc119979ffaadfbed6892fcab1e43155acc643e660f98dc67d86c89aaa5be8dbd9d3f4bba62ad25b1ab2342cc66dd09e40f26e23647d0e01c348511332ad6c7e33da504b0f8a4b4f00'

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

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

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

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

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

with open('res.txt','w',encoding='utf-8') as file:
    file.write(res.text)
    
comments_json = json.loads(res.text)
print(comments_json)
hot_comments = comments_json["hotComments"]
with open('hot_comments.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("---------------------------------------\n")
最佳答案
2020-7-26 08:03:49

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

把 hot_comments = comments_json["hotComments"] 改成 hot_comments = comments_json["data"]["hotComments"]即可:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 26 00:18:36 2020

@author: Chang YF
"""

import requests
import json

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',
    'referer': 'http://music.163.com/'}

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

encSecKey = 'b5295ea54521dd92971c8d7ec485ae485cd46e6ad78755856d559a497e82a697764d1659229c31c55a633bbc02f775a24e4df9b8b0019abc119979ffaadfbed6892fcab1e43155acc643e660f98dc67d86c89aaa5be8dbd9d3f4bba62ad25b1ab2342cc66dd09e40f26e23647d0e01c348511332ad6c7e33da504b0f8a4b4f00'

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

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

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

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

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

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

comments_json = json.loads(res.text)
print(comments_json)
hot_comments = comments_json['data']["hotComments"]
print(hot_comments)
with open('hot_comments.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("---------------------------------------\n")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

把 hot_comments = comments_json["hotComments"] 改成 hot_comments = comments_json["data"]["hotComments"]即可:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 26 00:18:36 2020

@author: Chang YF
"""

import requests
import json

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',
    'referer': 'http://music.163.com/'}

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

encSecKey = 'b5295ea54521dd92971c8d7ec485ae485cd46e6ad78755856d559a497e82a697764d1659229c31c55a633bbc02f775a24e4df9b8b0019abc119979ffaadfbed6892fcab1e43155acc643e660f98dc67d86c89aaa5be8dbd9d3f4bba62ad25b1ab2342cc66dd09e40f26e23647d0e01c348511332ad6c7e33da504b0f8a4b4f00'

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

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

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

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

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

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

comments_json = json.loads(res.text)
print(comments_json)
hot_comments = comments_json['data']["hotComments"]
print(hot_comments)
with open('hot_comments.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("---------------------------------------\n")
想知道小甲鱼最近在做啥?请访问 -> 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-17 14:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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