鱼C论坛

 找回密码
 立即注册
查看: 1226|回复: 4

爬取网易云音乐精彩评论

[复制链接]
发表于 2019-9-28 08:56:55 | 显示全部楼层 |阅读模式

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

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

x
根据小甲鱼的流程,打开网页,勾选disable cashe之后选取慢速加载页面,这里我只有fast 3g和slow 3g都是秒出页面,然后他出来的页面是播放窗口而不是刚才的评论界面,不过还是能找到精彩评论的那个文件,可是我双击那个文件之后他的网页打开地址并没有根据不同的歌曲而变始终是https://music.163.com/weapi/v1/resource/comments/get这样一个网址请问是为何
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-28 08:57:44 | 显示全部楼层
有人吗,不知道怎么发图片啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-28 10:14:27 | 显示全部楼层
不知道等级够不够
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-28 12:46:00 | 显示全部楼层
网址不是这个,抓包时把preserve log勾选上
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-28 17:15:58 | 显示全部楼层
  1. #!/usr/bin/env python2.7
  2. # -*- coding: utf-8 -*-
  3. # @Time   : 2019/3/1 3:20
  4. # @Author : Stubbron
  5. # @Email  : 1263270345@qq.com
  6. #参考链接:https://blog.csdn.net/qq_40721694/article/details/82356815
  7. #目标链接:https://music.163.com/song?id=460043703
  8. #未加密的API:http://music.163.com/api/v1/resource/comments/R_SO_4_460043703
  9. import requests,codecs
  10. import json
  11. import time
  12. from lxml import etree
  13. from wordcloud import WordCloud
  14. import numpy as np
  15. from PIL import Image
  16. import os
  17. import matplotlib.pyplot as plt

  18. class Config(object):
  19.     @staticmethod
  20.     def get_station_file_path(name):
  21.         currrent_path = os.path.realpath(__file__) #文件绝对路径
  22.         current_dir = os.path.split(currrent_path)[0]
  23.         result = current_dir +""+str(name)+ ".json"
  24.         return result
  25. class GetComments(object):

  26.     def __init__(self):
  27.         self.headers = {
  28.             'Referer': 'http://music.163.com/',
  29.             'Host': 'music.163.com',
  30.             'Accept-Language': "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
  31.             'Accept-Encoding': "gzip, deflate",
  32.             'Content-Type': "application/x-www-form-urlencoded",
  33.             'Origin': 'https://music.163.com',
  34.             'Connection': "keep-alive",
  35.             'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
  36.                           ' (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
  37.         }
  38.         self.session = requests.session()

  39.     def get_json(self,song_id, offset):
  40.         """
  41.         获取json数据
  42.         :param song_id: 歌曲id
  43.         :param offset: 评论偏移量
  44.         :return: json转成的dict
  45.         """
  46.         url = 'http://music.163.com/api/v1/resource/comments/R_SO_4_%s?limit=20&offset=%s' % (song_id, offset)
  47.         response = self.session.get(url=url,headers=self.headers).content
  48.         json_dict = json.loads(response)
  49.         return json_dict


  50.     def structure_url(self, song_id, song_name):
  51.         """
  52.         先获取评论总数,再分页爬取
  53.         :param song_id: 歌曲id
  54.         :param song_name: 歌曲名字
  55.         :return:
  56.         """
  57.         if os.path.exists(Config.get_station_file_path(song_name)):
  58.             print('正在读取 %s 的缓存评论' % songs_name)
  59.             with open(Config.get_station_file_path(song_name),"r",encoding="UTF-8") as f:
  60.                 result = json.loads(f.read(),encoding="UTF-8")
  61.             return result
  62.         else:
  63.             comments_list = []  #评论保存列表
  64.             json_dict = self.get_json(song_id, 0)
  65.             print(json_dict)
  66.             comments_num = int(json_dict['total'])  # 获取评论总数目
  67.             if not comments_num % 20:
  68.                 page = comments_num / 20
  69.             else:
  70.                 page = int(comments_num / 20) + 1
  71.             print("总计%s条评论"%(page * 20))
  72.             for i in range(page):
  73.                 json_dict = self.get_json(song_id, i * 20)
  74.                 for item in json_dict['comments']:
  75.                     comment = item['content'].replace("\n", "")  # 获取评论内容 并去掉换行符
  76.                     #liked_count = item['likedCount']  # 点赞总数
  77.                     #comment_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(item['time'] / 1000))  # 获取评论时间
  78.                     #comment_info = comment  #comment_time + ' ' + str(liked_count) + ' ' +
  79.                     comments_list.append(comment)
  80.                 print('第 %s 页获取完成.' % i)
  81.             with open(Config.get_station_file_path(song_name),"w",encoding="UTF-8") as f:
  82.                 json.dump(comments_list,f)
  83.             return comments_list

  84.     def wordcloud(self,name):
  85.         filr = ""
  86.         mask = np.array(Image.open("timg.jpg"))  # 矢量化图片
  87.         with open(Config.get_station_file_path(name), "r", encoding="UTF-8") as f:
  88.             result = json.loads(f.read(), encoding="UTF-8")
  89.         for i in result:
  90.             filr += i
  91.         # print(filr[:50])
  92.         # 生成词云
  93.         wordcloud = WordCloud(
  94.             random_state=4,
  95.             scale=8,  # 清晰度,越大越慢
  96.             background_color='white',  # 设置背景颜色
  97.             max_words=3000,  # 设置最大实现的字数
  98.             mask=mask,  # 加载图片
  99.             font_path=r'FZSTK.TTF',  # 设置字体格式,如不设置显示不了中文
  100.         ).generate(filr)
  101.         # 展示图片
  102.         plt.imshow(wordcloud)
  103.         plt.axis('off')  # 关闭x,y轴显示
  104.         plt.title("ciyun_one")  # 次元标题
  105.         plt.show()  # 展示词云

  106. if __name__ == '__main__':
  107.     singer_url = 'https://music.163.com/song?id=460043703'  # 记得要去掉#号,太那啥了
  108.     songs_name = "Perfect"
  109.     songs_id = "460043703"
  110.     spider = GetComments()
  111.     print('正在收集 %s 的评论' % songs_name)
  112.     json_dict = spider.structure_url(songs_id,songs_name)
  113.     print('准备生成 %s 的词云' % songs_name)
  114.     spider.wordcloud(songs_name)




复制代码


三月份的代码,不知道时效没有
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 23:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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