鱼C论坛

 找回密码
 立即注册
查看: 959|回复: 7

[已解决]爬网易云评论

[复制链接]
发表于 2020-7-28 16:53:25 | 显示全部楼层 |阅读模式

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

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

x
import requests
import json

def get_comment(res):
    comments_json = json.loads(res.text)
    comments=comments_json['data']
    with open('commments.txt','w',encoding='utf-8')as file:
        for each in comments:
            file.write(each['user']['nickname']+':\n\n')
            file.write(each['content']+'\n')
            file.write('---------------------------------------\n')



def get_url(url):
    headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0',
             'referer':'https://music.163.com/song?id=1435449062'}
    params= 'r1GlVgogiUtKOTFQgUibWP7uulMmPIKIhpw8mI4E3N/s2LaCmLD8whPWY+kxwsBPXVI5l1d/Gb2q6jKagpIucGGZnFgNxEcmQNYWk+LxPk6MU0CvOfDTyXBviTIpXJT69E61/H1vluuYvP0QqdobvK9/lrhbtVaM20TMy/AmkPIX+DhTRJs6ilwza+bLDdrQCcnFV69Il/L0plP6YG7/zdsqPTl0kVb9fXSLFID3AvvS0R4kwi3Eg+49ACtGl6N0Hcr1UBmzw9fdBEfnsr7Ws65eXpMZ6WKMw7oBUSuTYZg='
    encSecKey= 'd7ae3cb3961bbe48adc988202619a2da378a77107b4e4bb4dbc54685367e955f6e17a3b1d2881899de10b8fb2008de1c5549f2c605a3bac7b5d43dcde25a0e34d2edde250653c95e099de076ca0385106bd2c7beb74d3a1304039dfee811185e06d68dad4106c63bc321f90ea2cb3ccd83c1e24c1775021017fe8a41c0da60b9'
    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('please input the wangzhi:')
    res= get_url(url)
    get_comment(res)



if __name__=='__main__':
    main()

结果:
Traceback (most recent call last):
  File "D:\PYthon\练习\爬网易云.txt", line 37, in <module>
    main()
  File "D:\PYthon\练习\爬网易云.txt", line 32, in main
    get_comment(res)
  File "D:\PYthon\练习\爬网易云.txt", line 9, in get_comment
    file.write(each['user']['nickname']+':\n\n')
TypeError: string indices must be integers
网页数据的一部分:
{"code":200,"data":{"comments":[{"user":{"locationInfo":null,"liveInfo":null,"followed":false,"vipRights":null,"relationTag":null,"anonym":0,"userId":2083557743,"userType":0,"nickname":"林妤走丢了","avatarUrl":"https://p2.music.126.net/vFmEf8uG7U3YZMXSPysF7w==/109951164890108905.jpg","authStatus":0,"expertTags":null,"experts":null,"vipType":0,"remarkName":null,"isHug":false},"beReplied":[{"user":{"locationInfo":null,"liveInfo":null,"followed":false,"vipRights":null,"relationTag":null,"anonym":0,"userId":1840083527,"userType":0,"nickname":"你活该被丢下","avatarUrl":"https://p2.music.126.net/4H7NT5xFycRi65Ylp0boBw==/109951165153578197.jpg","authStatus":0,"expertTags":null,"experts":null,"vipType":0,"remarkName":null,"isHug":false},"beRepliedCommentId":3383739662,"content":"一个破账号你到底要看多久","status":0,"expressionUrl":null}],"commentId":3395142570,"content":"放不下的只有你  宝会过去的噢","status":0,"time":1595923404624,"likedCount":0,"liked":false,"expressionUrl":null,"parentCommentId":3383739662,"repliedMark":false,"pendantData":null,"showFloorComment":
最佳答案
2020-7-28 18:44:44
本帖最后由 1q23w31 于 2020-7-28 19:07 编辑
Hant 发表于 2020-7-28 17:38
网址https://music.163.com/#/song?id=1435449062

import requests
import json

def get_comment(res):
    comments_json = json.loads(res.text)
    comments=comments_json['data']['comments']
    with open('commments.txt','w',encoding='utf-8')as file:
        for each in comments:
            file.write(each['user']['nickname']+':\n\n')
            file.write(each['content']+'\n')
            file.write('---------------------------------------\n')



def get_url(url):
    headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0',
             'referer':'https://music.163.com/song?id=1435449062'}
    params= 'r1GlVgogiUtKOTFQgUibWP7uulMmPIKIhpw8mI4E3N/s2LaCmLD8whPWY+kxwsBPXVI5l1d/Gb2q6jKagpIucGGZnFgNxEcmQNYWk+LxPk6MU0CvOfDTyXBviTIpXJT69E61/H1vluuYvP0QqdobvK9/lrhbtVaM20TMy/AmkPIX+DhTRJs6ilwza+bLDdrQCcnFV69Il/L0plP6YG7/zdsqPTl0kVb9fXSLFID3AvvS0R4kwi3Eg+49ACtGl6N0Hcr1UBmzw9fdBEfnsr7Ws65eXpMZ6WKMw7oBUSuTYZg='
    encSecKey= 'd7ae3cb3961bbe48adc988202619a2da378a77107b4e4bb4dbc54685367e955f6e17a3b1d2881899de10b8fb2008de1c5549f2c605a3bac7b5d43dcde25a0e34d2edde250653c95e099de076ca0385106bd2c7beb74d3a1304039dfee811185e06d68dad4106c63bc321f90ea2cb3ccd83c1e24c1775021017fe8a41c0da60b9'
    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('please input the wangzhi:')
    res= get_url(url)
    get_comment(res)



if __name__=='__main__':
    main()
按这个代码,你没有解析对json
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-28 17:19:26 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-28 17:38:36 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-28 17:45:25 | 显示全部楼层


你输入的链接给一个测测吧

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

使用道具 举报

 楼主| 发表于 2020-7-28 18:30:38 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-28 18:44:44 | 显示全部楼层    本楼为最佳答案   
本帖最后由 1q23w31 于 2020-7-28 19:07 编辑
Hant 发表于 2020-7-28 17:38
网址https://music.163.com/#/song?id=1435449062

import requests
import json

def get_comment(res):
    comments_json = json.loads(res.text)
    comments=comments_json['data']['comments']
    with open('commments.txt','w',encoding='utf-8')as file:
        for each in comments:
            file.write(each['user']['nickname']+':\n\n')
            file.write(each['content']+'\n')
            file.write('---------------------------------------\n')



def get_url(url):
    headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0',
             'referer':'https://music.163.com/song?id=1435449062'}
    params= 'r1GlVgogiUtKOTFQgUibWP7uulMmPIKIhpw8mI4E3N/s2LaCmLD8whPWY+kxwsBPXVI5l1d/Gb2q6jKagpIucGGZnFgNxEcmQNYWk+LxPk6MU0CvOfDTyXBviTIpXJT69E61/H1vluuYvP0QqdobvK9/lrhbtVaM20TMy/AmkPIX+DhTRJs6ilwza+bLDdrQCcnFV69Il/L0plP6YG7/zdsqPTl0kVb9fXSLFID3AvvS0R4kwi3Eg+49ACtGl6N0Hcr1UBmzw9fdBEfnsr7Ws65eXpMZ6WKMw7oBUSuTYZg='
    encSecKey= 'd7ae3cb3961bbe48adc988202619a2da378a77107b4e4bb4dbc54685367e955f6e17a3b1d2881899de10b8fb2008de1c5549f2c605a3bac7b5d43dcde25a0e34d2edde250653c95e099de076ca0385106bd2c7beb74d3a1304039dfee811185e06d68dad4106c63bc321f90ea2cb3ccd83c1e24c1775021017fe8a41c0da60b9'
    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('please input the wangzhi:')
    res= get_url(url)
    get_comment(res)



if __name__=='__main__':
    main()
按这个代码,你没有解析对json
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-7-28 19:56:46 | 显示全部楼层
1q23w31 发表于 2020-7-28 18:44
按这个代码,你没有解析对json

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

使用道具 举报

 楼主| 发表于 2020-7-28 20:01:06 | 显示全部楼层
1q23w31 发表于 2020-7-28 18:44
按这个代码,你没有解析对json

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-19 17:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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