爬网易云评论
import requestsimport 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":
网址是https://music.163.com/#/song?id=1435449062 网址https://music.163.com/#/song?id=1435449062
你输入的链接给一个测测吧
Twilight6 发表于 2020-7-28 17:45
你输入的链接给一个测测吧
https://music.163.com/#/song?id=1435449062 本帖最后由 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 1q23w31 发表于 2020-7-28 18:44
按这个代码,你没有解析对json
可以了
1q23w31 发表于 2020-7-28 18:44
按这个代码,你没有解析对json
感谢老哥
页:
[1]