日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫网易云音乐最热评论并分析_网易云音乐热门评论api分析

發布時間:2023/12/14 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫网易云音乐最热评论并分析_网易云音乐热门评论api分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

網上有現成的例子我就扒過來了!!

title: 網易云評論api分析

date: 2018-12-24 20:54:46

tags: [python]

網易云音樂是個好地方,里面各個都是人才,特別是評論區……

所以我就想把評論爬下來看看,下面記錄一下分析api的過程與結果。

過程大概如下:

定位到獲取評論的請求

分析評論請求方式

用python獲取評論

定位評論請求

廢話少說,F12,在網頁中隨便找一首歌,url形如: https://music.163.com/#/song?id=28828076,抓包。

待頁面加載完成之后,隨便找條評論搜索,定位到獲取評論的那條請求,如下圖:

可以看到這條請求是發向https://music.163.com/weapi/v1/resource/comments/R_SO_4_28828076?csrf_token=的,而其中的R_SO_4_后面其實就是歌曲的id。

分析請求

請求中發送的數據如下圖,看一眼,啥都看不懂,肯定是加密過了。

從表單中的名稱猜測出一個是加密后的內容,另一個是加密密鑰。

下面要找到在哪里加密的信息,然后進一步找到加密方法,這樣我們就可以用爬蟲來模擬了。

在哪加密

同樣,在network tab下面搜索encSecKey,搜到了一個js文件中的兩個函數都有encSecKey。

下面列出這兩個函數:

函數一

! function() {

function a(a) {

// 獲取隨機串

var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",

c = "";

for (d = 0; a > d; d += 1) e = Math.random() * b.length, e = Math.floor(e), c += b.charAt(e);

return c

}

// a是待加密數據,b是passphrase

function b(a, b) {

var c = CryptoJS.enc.Utf8.parse(b),

d = CryptoJS.enc.Utf8.parse("0102030405060708"),

e = CryptoJS.enc.Utf8.parse(a),

// 使用AES加密

// e是待加密信息

// c是passphrase

// d是nonce

f = CryptoJS.AES.encrypt(e, c, {

iv: d,

mode: CryptoJS.mode.CBC

});

return f.toString()

}

// 生成RSA密鑰對

function c(a, b, c) {

var d, e;

return setMaxDigits(131), d = new RSAKeyPair(b, "", c), e = encryptedString(d, a)

}

function d(d, e, f, g) {

// d是表單數據

// e和g是常數

// 經過分析e: 010001

// g: 0CoJUm6Qyw8W8jud

var h = {},

i = a(16); // 長度為16的隨機串

// 上面的b函數是用于AES加密,這里調用b進行了兩次AES加密

// 使用

return h.encText = b(d, g), h.encText = b(h.encText, i), h.encSecKey = c(i, e, f), h

}

function e(a, b, d, e) {

var f = {};

return f.encText = c(a + e, b, d), f

}

window.asrsea = d, window.ecnonasr = e

}();

函數二

(function() {

var c9h = NEJ.P,

et2x = c9h("nej.g"),

v9m = c9h("nej.j"),

k9b = c9h("nej.u"),

Xw8o = c9h("nm.x.ek"),

l9c = c9h("nm.x");

if (v9m.bl9c.redefine) return;

window.GEnc = true;

var brK4O = function(cxx0x) {

var m9d = [];

k9b.be9V(cxx0x, function(cxv0x) {

m9d.push(Xw8o.emj[cxv0x])

});

return m9d.join("")

};

var cxt0x = v9m.bl9c;

v9m.bl9c = function(Y9P, e9f) {

var i9b = {},

e9f = NEJ.X({}, e9f),

mt4x = Y9P.indexOf("?");

if (window.GEnc && /(^|\.com)\/api/.test(Y9P) && !(e9f.headers && e9f.headers[et2x.Cf0x] == et2x.Gl1x) && !e9f.noEnc) {

if (mt4x != -1) {

i9b = k9b.hb3x(Y9P.substring(mt4x + 1));

Y9P = Y9P.substring(0, mt4x)

}

if (e9f.query) {

i9b = NEJ.X(i9b, k9b.fP2x(e9f.query) ? k9b.hb3x(e9f.query) : e9f.query)

}

if (e9f.data) {

i9b = NEJ.X(i9b, k9b.fP2x(e9f.data) ? k9b.hb3x(e9f.data) : e9f.data)

}

i9b["csrf_token"] = v9m.gO3x("__csrf");

Y9P = Y9P.replace("api", "weapi");

e9f.method = "post";

delete e9f.query;

var bVs3x = window.asrsea(JSON.stringify(i9b), brK4O(["流淚", "強"]), brK4O(Xw8o.md), brK4O(["愛心", "女孩", "驚恐", "大笑"]));

e9f.data = k9b.cB0x({

params: bVs3x.encText,

encSecKey: bVs3x.encSecKey

})

}

cxt0x(Y9P, e9f)

};

v9m.bl9c.redefine = true

})();

函數二調用了window.asrsea,而window.asrsea又在函數一中被賦值為d。

加密參數

在window.asrsea的參數表中,發現有兩個硬編碼的列表經過某個函數轉換后傳入到window.asrsea,也就是傳入到d中,在d打下斷點,發現經過轉換后的傳入到d中的參數如下:

d: "{"ids":"[5113327]","br":128000,"csrf_token":""}"

e: "010001"

f: "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"

g: "0CoJUm6Qyw8W8jud"

現在兩個常數e,g都已經確定了,經過多次斷點,發現f竟然也是常數。

那么現在傳入加密函數d中的所有參數都已經確定了,接下來要分析出來怎么用這些參數加密數據,然后發送到服務器。

加密策略

加密策略是使用AES加密請求數據,然后用RSA加密AES密鑰,我嘗試分析了一下,發現分析得頭皮發麻,后面在github找到了。。。。。鏈接在這:網易云新版webapi分析

代碼

代碼是python2寫的,改了一下,python3能用:

import requests

import os

import binascii

import base64

import requests

import json

from Crypto.Cipher import AES

modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'

nonce = b'0CoJUm6Qyw8W8jud'

pubKey = '010001'

"""

加密過程

隨機生成加密密鑰secKey

然后用secKey兩次對post的查詢數據進行兩次AES加密

用RSA對secKey加密

最后把加密后的secKey和查詢數據post到服務器

"""

def createSecretKey(size):

return binascii.hexlify(os.urandom(size))[:16]

def aesEncrypt(text, secKey):

pad = 16 - len(text) % 16

text = text + pad * str(chr(pad))

encryptor = AES.new(secKey, AES.MODE_CBC, '0102030405060708'.encode('utf8'))

ciphertext = encryptor.encrypt(text.encode('utf8'))

ciphertext = base64.b64encode(ciphertext).decode('utf8')

return ciphertext

def rsaEncrypt(text, pubKey, modulus):

text = text[::-1]

rs = int(bytes.hex(text), 16) ** int(pubKey, 16) % int(modulus, 16)

return format(rs, 'x').zfill(256)

def encrypted_request(text):

text = json.dumps(text)

secKey = createSecretKey(16)

encText = aesEncrypt(aesEncrypt(text, nonce), secKey)

encSecKey = rsaEncrypt(secKey, pubKey, modulus)

data = {

'params': encText,

'encSecKey': encSecKey

}

return data

def load_headers(filename):

headers = {}

with open(filename, 'r') as f:

for line in f.readlines():

entry = line.split(':', 1)

if len(entry) != 2:

continue

headers[entry[0].strip()] = entry[1].strip()

return headers

def comment_post_url(song_id, csrf=''):

return 'https://music.163.com/weapi/v1/resource/comments/R_SO_4_' + \

song_id + '?csrf_token=' + csrf

if __name__ == '__main__':

# 加載請求頭

headers = load_headers('headers')

song_id = '28828074'

req = {

"csrf_token":"",

"limit":"20",

"offset":"0",

"rid":"R_SO_4_" + song_id,

"total":"true"

}

url = comment_post_url(song_id)

postdata = encrypted_request(req)

print('sending to {}'.format(url))

r = requests.post(url, data=postdata, headers=headers)

print(r.text)

經過測試Cookies中只有帶有_ntes_nuid就行了。

headers:

Host: music.163.com

Connection: keep-alive

Origin: https://music.163.com

User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/71.0.3578.80 Chrome/71.0.3578.80 Safari/537.36

Content-Type: application/x-www-form-urlencoded

Accept: */*

Referer: https://music.163.com/song?id=558290126

Accept-Encoding: gzip, deflate, br

Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6

Cookie: _ntes_nuid=9ae4bf42cfdf19018c7b461442deffe2

參考資料

總結

以上是生活随笔為你收集整理的python爬虫网易云音乐最热评论并分析_网易云音乐热门评论api分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。