爬取网易云音乐热门评论地址一样该怎么办
图片如下:代码如下:
# 爬取网易云音乐的热门评论
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['content'] + '\n')
file.write("----------------------------------------\n")
def get_comments(url):
# 传给它referer
# 当然,有时间的话将headers头部填写完整,那样会更好一些
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
'referer':'http://music.163.com/'
}
params = 'qwlzsLiNsFKaTuA35GUvfSbubkyePTPj2UxjaTe6KGyPkHwZGR48+RklK+UytoBSpYzR1208h4cx3iuy3nAVmsWI/L2rh8m+AZQ5dJhk+CrymGZdidV5OzDOBeKpqpIWZCCo2KgCoiRdXQ7vB2xwwZ32Qw8p0f3/9n/s3CnYDvnA/d3aVb8VyjtRaSd13ilnkHkt1f+Iadh27Vp9Fw62nQ1jdH7tswHEUmCm0+daLK73JYv1dSk4CnD6OqwgAtxeDih31OE4hP1cYrT2wS+ukA=='
encSecKey = '88a3e4f6e67fcd26e48988f1129187acfc6cfa14f873c9448c05d0fef2ed52c0825076672a8a115141b51ec15e00402b6105464871ae1bfcdd917f4d4a75093f3366a528cf655a0ac17fb9cce585e3c3e418cf8871844e78a3710a3353c53a50289fcf436a678c753a51a4d7cb45120c3d8a902a903d768d2b9ad88e6958743d'
data = {
"params":params,
"encSecKey":encSecKey
}
res = requests.post(url, headers=headers, data=data)
return res
def main():
url = input("请输入链接地址:(请包含http或https协议):")
res = get_comments(url)
get_hot_comments(res)
if __name__=="__main__":
main() 本帖最后由 isdkz 于 2023-4-22 22:36 编辑
有一些接口不是通过get参数来调用的,所以url就没有区别,只有get参数才能在url上体现,这个是通过POST参数 params 和 encSecKey 来判断获取哪首音乐的字幕,
而 params 和 encSecKey 是在前端 js 代码中动态构造出来的,需要有一定的前端js功底来逆向构造逻辑,
你可以看一下这篇文章:https://blog.csdn.net/Estelle1412/article/details/121626794
页:
[1]