小小鹏 发表于 2020-9-2 09:29:36

网易云爬取评论

import requests
import json

def get_hot_comments(res):
    comments_json = json.loads(res.text)
    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")
         
def get_comments(url):
    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 = "T+DLg45OzA37ToVvomOX3flCnoMo/F/wTQ4NpK5BF7I1Mwiy25qEv0c5vUhfdBnYnyF9IVIMyadHk0uQbg6eIjJelT/KRk+m8FEuY3RF0wsbw737ngQF0tvNKNrydbkvg/2v4sVn52pTkbVUlpknS5h4tGmmLSfxrPTDYx3o4AqY0GRLIaciHS9ajCvVkXirwYsCB0wdohQVFWoCf199g8p9nCV5Xf1tOURhDH1JavyliGgvkRn4rjtcIwFIFBk4ABOU0/1CdupZPpfGfcM6oQ=="
    encSecKey = "74860fc8b66565efdf1630ee6bc6c0b657a725320b4507a0eb80bfabac7bdc0abf176ac70ea219b4cfb1f4970e54d63e580a6086f68d73467b7e11c471b5774a79c19af2ba174a50395ae28da4df959ed2cbd2fc38dcd48b40384a079346b172e3fab7157585e372e6d85ccc322eddde0d43f3a8b4c04f4204b8d16ab413ceda"   
    data = {
      "params": params,
      "encSecKey": encSecKey
      }

    target_url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="   
    res = requests.post(target_url, headers=headers, data=data)   
    return res

def main():
    url = input("请输入链接地址:")
    res = get_comments(url)
    get_hot_comments(res)

if __name__ == "__main__":
    main()

当我运行的时候,就会报错,请问这是什么原因呢?
https://pic.downk.cc/item/5f4ef56d160a154a67cc8a61.png

1q23w31 发表于 2020-9-2 09:31:43

import requests
import json

def get_hot_comments(res):
    comments_json = json.loads(res.text)
    hot_comments = comments_json['data']['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")
         
def get_comments(url):
    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 = "T+DLg45OzA37ToVvomOX3flCnoMo/F/wTQ4NpK5BF7I1Mwiy25qEv0c5vUhfdBnYnyF9IVIMyadHk0uQbg6eIjJelT/KRk+m8FEuY3RF0wsbw737ngQF0tvNKNrydbkvg/2v4sVn52pTkbVUlpknS5h4tGmmLSfxrPTDYx3o4AqY0GRLIaciHS9ajCvVkXirwYsCB0wdohQVFWoCf199g8p9nCV5Xf1tOURhDH1JavyliGgvkRn4rjtcIwFIFBk4ABOU0/1CdupZPpfGfcM6oQ=="
    encSecKey = "74860fc8b66565efdf1630ee6bc6c0b657a725320b4507a0eb80bfabac7bdc0abf176ac70ea219b4cfb1f4970e54d63e580a6086f68d73467b7e11c471b5774a79c19af2ba174a50395ae28da4df959ed2cbd2fc38dcd48b40384a079346b172e3fab7157585e372e6d85ccc322eddde0d43f3a8b4c04f4204b8d16ab413ceda"   
    data = {
      "params": params,
      "encSecKey": encSecKey
      }

    target_url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="   
    res = requests.post(target_url, headers=headers, data=data)   
    return res

def main():
    url = input("请输入链接地址:")
    res = get_comments(url)
    get_hot_comments(res)

if __name__ == "__main__":
    main()
json解析错误,试试这样

小小鹏 发表于 2020-9-2 10:29:51

1q23w31 发表于 2020-9-2 09:31
json解析错误,试试这样

谢谢,可以正常运行了,我错误的原因是列表中读取hotcomments失败。

页: [1]
查看完整版本: 网易云爬取评论