python爬取网易云音乐评论

yipeiwu_com6年前Python爬虫

本文实例为大家分享了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python爬取视频(其实是一篇福利)过程解析

窗外下着小雨,作为单身程序员的我逛着逛着发现一篇好东西,来自知乎 你都用 Python 来做什么?的第一个高亮答案。 到上面去看了看,地址都是明文的,得,赶紧开始吧。 下载流式文件,re...

Python爬虫利用cookie实现模拟登陆实例详解

Python爬虫利用cookie实现模拟登陆实例详解

Cookie,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)。 举个例子,某些网站是需要登录后才能得到你想要的信息的,不登陆只能是游客模式...

Python开发实例分享bt种子爬虫程序和种子解析

看到网上也有开源的代码,这不,我拿来进行了二次重写,呵呵,上代码:  #encoding: utf-8     &n...

python爬虫爬取幽默笑话网站

python爬虫爬取幽默笑话网站

爬取网站为:http://xiaohua.zol.com.cn/youmo/ 查看网页机构,爬取笑话内容时存在如下问题: 1、每页需要进入“查看更多”链接下面网页进行进一步爬取内容每页查...

Python网络爬虫之爬取微博热搜

Python网络爬虫之爬取微博热搜

微博热搜的爬取较为简单,我只是用了lxml和requests两个库 url= https://s.weibo.com/top/summary?Refer=top_hot&topnav=1...