python爬取网易云音乐评论
程序员文章站
2023-11-20 08:55:04
本文实例为大家分享了python爬取网易云音乐评论的具体代码,供大家参考,具体内容如下
import requests
import bs4
import...
本文实例为大家分享了python爬取网易云音乐评论的具体代码,供大家参考,具体内容如下
import requests import bs4 import json def get_hot_comments(res): comments_json = json.loads(res.text) hot_comments = comments_json['hotcomments'] with open("hotcmments.txt", 'w', encoding = 'utf-8') as f: for each in hot_comments: f.write(each['user']['nickname']+':\n') f.write(each['content']+'\n\n') f.write("-------------------------------------\n") def open_url(url): rname_id = url.split('=')[1] headers={"user-agent":"mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/55.0.2883.87 safari/537.36", "referer": "http://music.163.com/song?id=4466775&market=baiduqk"} params = "pwxgrrpqkqzfgf4qteivq9ezfrcscy2ytka60xw6p6kl6v4j09c/g+pnwzks+mpwudmjdwvj0cnfv/vzeh0ilnivywz+9weztesdc2/lppkgcsgfo8au3evls5opcilmvg7yghefioczq/ccgafdg4wdqstjpdeiobfzegzjzisixw0sg4zvhbrfgkti0i22" encseckey = "61be0f8c5305c919985b294069695d2ba84746c75ed902e8157b6b595a920c57cfedf552f5c764fed37be84bfd1cce31e05eb364644930fbe6bc074747ed8e670933aef4d8b8841209c6956f4b532f8a3caadfaffb61f233a42e53dc5795183b9c6ccb30b8aa56d656466cc6523e8213560bb3e476ab95d58755f47f91cf7f53" data ={ "params": params, "encseckey": encseckey } target_url = "http://music.163.com/weapi/v1/resource/comments/r_so_4_{}??csrf_token=".format(rname_id) res = requests.post(target_url, headers = headers,data = data) return res def main(): #url = input("请输入您需要获取的歌曲地址:") url = "http://music.163.com/#/song?id=4466775" res = open_url(url) get_hot_comments(res) #with open("res.txt",'w', encoding = 'utf-8') as f: # f.write(res.text) if __name__ == "__main__": main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Oracle函数、子程序实例讲解