cyff 发表于 2020-7-26 01:03:42

关于爬取网易云音乐评论的错误

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

# -*- 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('=')

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")

Twilight6 发表于 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('=')

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")

ferious 发表于 2020-11-24 18:08:57

Twilight6 发表于 2020-7-26 08:03
hotComments 键,在 data 里面,你需要先读取 data ,然后在获取 hotComments

把 hot_comments = com ...

帮我看看这个?
页: [1]
查看完整版本: 关于爬取网易云音乐评论的错误