鱼C论坛

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

[已解决]网易云爬取评论

[复制链接]
发表于 2020-9-2 09:29:36 | 显示全部楼层 |阅读模式

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

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

x
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()

当我运行的时候,就会报错,请问这是什么原因呢?

                               
登录/注册后可看大图
最佳答案
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解析错误,试试这样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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解析错误,试试这样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-2 10:29:51 | 显示全部楼层
1q23w31 发表于 2020-9-2 09:31
json解析错误,试试这样

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

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-18 20:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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