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

找到网易云的评论页面,打开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']

运行得到如下结果:

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

点赞