栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

抓取网易云音乐的热门评论

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

抓取网易云音乐的热门评论

找到网易云的评论页面,打开F12,找到评论那条json数据。

可以看到这有两条参数,这俩条参数都是经过加密的,要想找多首歌的热门评论,就需要破解它的加密算法

算法控制由js来控制的,找到那个js文件,也就是core.js文件

分析它的js。

可以看到他是由bua.encText和bua,encSecKey来控制
那就找bua
在这句的上面就有bua ,是由window.asrsea这个函数得到,有四个参数。
先取第一个参数。

同理剩下四个参数也都能取到
那么这四个参数如何得到params和encSecKey,继续在js文件里找。

由b函数进过俩次加密得到
那么函数b是什么呢?

得到密钥偏移量iv0102030405060708模式CBC
那么就不难写出它的加密算法了。

```#!/usr/bin/env python
# coding=utf-8
# @Author: Ljs
# @Date:   2017-04-11 12:30:12
# @Last Modified by:   Administrator
# @Last Modified time: 2017-04-11 16:38:36

import requests
import json
import os
import base64
from Crypto.Cipher import AES

def aesEncrypt(text, secKey): #加密

    pad = 16 - len(text) % 16
    text = text + pad * chr(pad)
    encryptor = AES.new(secKey, 2, '0102030405060708')
    ciphertext = encryptor.encrypt(text)
    ciphertext = base64.b64encode(ciphertext)
    return ciphertext #密文
    print ciphertext

def rsaEncrypt(text, pubKey, modulus):
    text = text[::-1]
    rs = int(text.encode('hex'), 16)**int(pubKey, 16) % int(modulus, 16)
    return format(rs, 'x').zfill(256)

def createSecretKey(size): #生成长度为16的随机字符串
    return (''.join(map(lambda xx: (hex(ord(xx))[2:]), os.urandom(size))))[0:16]

url = 'http://music.163.com/weapi/v1/resource/comments/R_SO_4_30953009/?csrf_token='
headers = {
    'cookie': 'appver=1.5.0.75771;',
    'Referer': 'http://music.163.com/'
}
text = {
    'rid':'R_SO_4_30953009',
    'offset':'0',
    'total':'true',
    'limit':'20',
    'csrf_token':'',
}
modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
nonce = '0CoJUm6Qyw8W8jud'
pubKey = '010001'
text = json.dumps(text) #转化成字符串str
secKey = createSecretKey(16)
encText = aesEncrypt(aesEncrypt(text, nonce), secKey)
encSecKey = rsaEncrypt(secKey, pubKey, modulus)
data = {
    'params': encText,
    'encSecKey': encSecKey
}

req = requests.post(url, headers=headers, data=data)

for content in req.json()['hotComments']:
    print content['content'].encode('utf-8')
print req.json()['total']

运行得到如下结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/225676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号