您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何使用JS逆向方法”,在日常操作中,相信很多人在如何使用JS逆向方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用JS逆向方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
就是一個(gè) html 表格, 熟悉的應(yīng)該就直接能看出來, 然后還有img標(biāo)簽, a標(biāo)簽之類的。
當(dāng)我們用chrome瀏覽器 (推薦用谷歌) 進(jìn)入網(wǎng)易云官網(wǎng),找到一首你喜歡的歌。
打開 f12 功能, 點(diǎn)擊 XHR 過濾, 這個(gè)時(shí)候,我們點(diǎn)擊播放, 在右側(cè)就會重新捕獲到新的網(wǎng)絡(luò)請求,其中就包括我們需要的歌曲文件鏈接。就像這樣。
v1?csrf … 這個(gè)網(wǎng)址就是剛刷的, 在響應(yīng)中可以看到,有個(gè)url,你復(fù)制打開,就可以直接播放, 我們點(diǎn)擊一下headers看看怎么發(fā)送的。
請求了request url , 用post發(fā)送, 下面有2個(gè)參數(shù)表單 params 和 encSecKey 貌似我們有下面2個(gè)參數(shù)就可以直接發(fā)送請求了, 所以直接就嘗試了一下。
def spider(self): """ 這是爬取一首歌的方式, 復(fù)制params就可以發(fā)送請求 """ r = requests.post(self.params_url, params=self.params) if r.status_code == 200: mp3 = r.json().get("data")[0].get("url") rmp3 = requests.get(mp3, headers={"user-agent": self.ua}) if rmp3.status_code == 200: with open("像魚.mp3", 'wb') as ;fw: ;fw.write(rmp3.content) print("下載成功")
最后成功下載。
也就是說,我們只需要知道這二個(gè)參數(shù)怎么來的,就可以自己構(gòu)造了,那就想怎么就怎么了,這個(gè)時(shí)候,我們就可以打開瀏覽器自帶的調(diào)試功能了。要打斷點(diǎn),要debug, 怎么打,怎么斷? 仔細(xì)點(diǎn)看我圖的注釋就可以了。
還是之前的包, 你點(diǎn)擊第四個(gè) initiator 就會刷新出很多和他有關(guān)系的文件, 我們點(diǎn)擊第一個(gè)。
然后就來到這樣, 還記得之前的二個(gè)參數(shù)吧, 在這里我們直接 ctrl + f 找其中的一個(gè)參數(shù)。
這里可以看到 params , encSecKey 都是根據(jù) bvz7s 來的, 而bvz7s 是根據(jù) window.asrsea() 函數(shù)來的, 所以在這個(gè) 函數(shù)打一個(gè)斷點(diǎn), 繼續(xù)看下一個(gè)搜索點(diǎn)
在這里,我們發(fā)現(xiàn)window.asrsea = d 所以就得看 d 函數(shù),在d函數(shù)的語氣中,我們都可以打上斷點(diǎn),以便觀察清楚。 打上斷電之后, 刷新頁面,等待一段時(shí)間。
之后就到第一個(gè)斷點(diǎn)處, 然后 f8 跳到下一個(gè)斷點(diǎn)
然后就可以發(fā)現(xiàn) d 接受的4個(gè)參數(shù)是什么了, (d, e, f, g) 在右側(cè)我們也可以看到,多次測試發(fā)現(xiàn),后面三個(gè)是加密參數(shù),固定值,所以復(fù)制拿過來用就可以了, 對于第一個(gè)d = {“csrf_token”:"…"} 這個(gè)是用來記錄你是否登入賬號, 如果你沒有登陸, 那就是空。
繼續(xù)f8 跳轉(zhuǎn)到最后
發(fā)現(xiàn)就是把最開始接受的4個(gè)參數(shù),然后經(jīng)過a, b, b, c 函數(shù)處理就可以了,那待會我們就要看看每個(gè)函數(shù)有什么作用,這就涉及到他們的加密方式了,但是在這里,就要思考一個(gè)問題了,關(guān)于最開始的4個(gè)參數(shù), 就一個(gè)d會變, 其他都沒變化, 而且d還是一個(gè)空或者亂七八糟的的數(shù)字, 那他是怎么知道我是哪一首歌? 哪個(gè)歌手,所以這個(gè)參數(shù)一定有問題, (后面經(jīng)過加密測試,發(fā)現(xiàn)加密后參數(shù)長度少了很多) 所以在這里我就繼續(xù) 調(diào)試了一下, 一樣的操作。
調(diào)試一圈了,最后終于有一個(gè)靠譜的了,有歌曲的id 還有歌曲的音質(zhì), 其他的,如果不熟悉,可以每一個(gè)d都去試試,直到加密參數(shù)正確。
所以先確定d為
"{"ids":"[1459950258]","level":"standard","encodeType":"aac","csrf_token":"59098e191e8babbaef83f1b8bbbe5987"}"
姑且就用這個(gè)d參數(shù)去加密嘗試一次吧。
回到之前d函數(shù)的區(qū)域,就在d的上面,我們就可以看到a,b,c 函數(shù)的執(zhí)行過程。
我們只需要一個(gè)個(gè)了解好,然后用python語言轉(zhuǎn)換一下就可以了。下面分模塊講這些。
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函數(shù)接受一個(gè)a參數(shù), 然后再一次循環(huán)中, 循環(huán)一次為a次, 然后從 b中 隨機(jī)的挑選一些字符, 最后用字符串的形式返回, 對于Javascript來說,隨機(jī)沒那么容易,他需要用 random 生成 (0, 1) 的數(shù),然后放大,取整,取值,累加,但對于python來說, 如下:
def SimulateFunctionA(self, length=None): """ @JavaScript 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 } length : 16 using the python get the c """ b = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' c = random.sample(b, length) return "".join(c)
簡單.
function b(a, b) { var c = CryptoJS.enc.Utf8.parse(b) , d = CryptoJS.enc.Utf8.parse("0102030405060708") , e = CryptoJS.enc.Utf8.parse(a) , f = CryptoJS.AES.encrypt(e, c, { iv: d, mode: CryptoJS.mode.CBC }); return f.toString() }
這是一個(gè) AES 加密, 模式 CBC, 其實(shí)剛開始我也不知道AES加密是什么東西, 后面我查看了官網(wǎng)文檔,參考了其他的辦法,實(shí)現(xiàn)了。
觀察這個(gè)函數(shù), 接受了a,b, 其中a,b 是什么可以再函數(shù)d中看到到。
根據(jù)之前的分析, g是固定值,我們已經(jīng)復(fù)制下來, d 認(rèn)為是一個(gè)字符字典
"{"ids":"[1459950258]","level":"standard","encodeType":"aac","csrf_token":"59098e191e8babbaef83f1b8bbbe5987"}"
這樣,我們用python語言加入這些參數(shù),試著模擬一下。
def SimulateFunctionB(self, d, key): """ function b(a, b) { var c = CryptoJS.enc.Utf8.parse(b) , d = CryptoJS.enc.Utf8.parse("0102030405060708") , e = CryptoJS.enc.Utf8.parse(a) , f = CryptoJS.AES.encrypt(e, c, { iv: d, mode: CryptoJS.mode.CBC }); return f.toString() } a = `"{"ids":"[1459950258]","level":"standard","encodeType":"aac","csrf_token":"59098e191e8babbaef83f1b8bbbe5987"}"` b = key = self.g(first) = SimulateFunctionA()(second) """ key = key.encode() iv = self.iv.encode() aes = AES.new(key=key, mode=AES.MODE_CBC, iv=iv) text = pad(data_to_pad=d.encode(), block_size=AES.block_size) aes_text = aes.encrypt(plaintext=text) aes_texts = base64.b64encode(aes_text).decode() return aes_texts SimulateFunticonB(d=" `"{"ids":"[1459950258]","level":"standard","encodeType":"aac","csrf_token":"59098e191e8babbaef83f
這里也是成功實(shí)現(xiàn)了,截圖我忘記截了。 關(guān)于如何AES加密,可以直接看我的,有時(shí)間有興趣的也可以和我一樣看官網(wǎng)文檔。 都行, 實(shí)現(xiàn)就可以了。
function c(a, b, c) { var d, e; return setMaxDigits(131), d = new RSAKeyPair(b,"",c), e = encryptedString(d, a) }
一看很簡單,其實(shí)復(fù)雜, 用到了RSA加密算法,關(guān)于RSA加密算法,我找了一些資料。
大致原理如圖:
參考文檔
我們用python這樣實(shí)現(xiàn);
def SimulateFunctionC(self, random16): """ a = 131 RSA加密原理 # num = pow(x, y) % z # 加密C=M^e mod n """ e = self.e f = self.f text = random16[::-1] num = pow(int(text.encode().hex(), 16), int(e, 16), int(f, 16)) return format(num, 'x').zfill(131) # TODO: last the num change the hex digit and left fill (131)
pow() 其實(shí)是可以接受三個(gè)參數(shù)的, 如果有第三個(gè), 第三個(gè)就為取余值, 用上int(a, 16) 就可以直接將16進(jìn)制轉(zhuǎn)換為10進(jìn)制, 最后的format(num, ‘x’) 將值用16進(jìn)制形式輸出, 然后zfill() 填充131 位數(shù),, (根據(jù)函數(shù)C得知 位數(shù)為131)
分析了上面三個(gè)函數(shù), 其實(shí)我們就可以直接編寫程序加密了, 我們把程序連起來。
# -*- coding : utf-8 -*- # @Time : 2020/9/15 15:45 # @author : 沙漏在下雨# @Software : PyCharm# @CSDN : https://me.csdn.net/qq_45906219import requestsfrom get_useragent import GetUserAgentCSimport randomfrom Crypto.Util.Padding import padfrom Crypto.Cipher import AESimport base64class GetParams(object): def __init__(self): self.ua = GetUserAgentCS().get_user() self.params_url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=' self.e = "010001" self.g = "0CoJUm6Qyw8W8jud" self.iv = '0102030405060708' self.f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a" \ "876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9" \ "d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e28" \ "9dc6935b3ece0462db0a22b8e7" self.params = None def SimulateFunctionA(self, length=None): """ @JavaScript 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 } length : 16 using the python get the c """ b = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' c = random.sample(b, length) return "".join(c) def SimulateFunctionB(self, d, key): """ function b(a, b) { var c = CryptoJS.enc.Utf8.parse(b) , d = CryptoJS.enc.Utf8.parse("0102030405060708") , e = CryptoJS.enc.Utf8.parse(a) , f = CryptoJS.AES.encrypt(e, c, { iv: d, mode: CryptoJS.mode.CBC }); return f.toString() } a = "{"csrf_token":""}" b = key = self.g(first) = SimulateFunctionA()(second) """ key = key.encode() iv = self.iv.encode() aes = AES.new(key=key, mode=AES.MODE_CBC, iv=iv) text = pad(data_to_pad=d.encode(), block_size=AES.block_size) aes_text = aes.encrypt(plaintext=text) aes_texts = base64.b64encode(aes_text).decode() return aes_texts def SimulateFunctionC(self, random16): """ a = 131 RSA加密原理 # num = pow(x, y) % z # 加密C=M^e mod n """ e = self.e f = self.f text = random16[::-1] num = pow(int(text.encode().hex(), 16), int(e, 16), int(f, 16)) return format(num, 'x').zfill(131) # TODO: last the num change the hex digit and left fill (131) def spider(self): """ 這是爬取一首歌的方式, 復(fù)制params就可以發(fā)送請求 """ r = requests.post(self.params_url, params=self.params) if r.status_code == 200: mp3 = r.json().get("data")[0].get("url") rmp3 = requests.get(mp3, headers={"user-agent": self.ua}) if rmp3.status_code == 200: with open("像魚.mp3", 'wb') as ;fw: ;fw.write(rmp3.content) print("下載成功") def get_encrypt_params(self, d=None): """ The function can encrypt your params if you give me a d @params: d debug your chrome browser """ i = self.SimulateFunctionA(length=16) encText = self.SimulateFunctionB(d, self.g) encText = self.SimulateFunctionB(encText, i) encSecKey = self.SimulateFunctionC(random16=i) return { "params": encText, "encSecKey": encSecKey }# a = GetParams()# a.spider()
說到底,我們還是要歌曲的id, 怎么來的,就需要繼續(xù)看下去了。
直接這樣就可以了。
如果你是進(jìn)入歌手表單在這個(gè)界面,你是找不到需要的id表單數(shù)據(jù)的,在這里就要用selenium 去爬取然后分析了,
如果你在下面的情況下,就可以找到id表單數(shù)據(jù)。
還有一樣的,在這個(gè)包,我們看到參數(shù)還是params 和 encSerKey 然后重復(fù)上面操作, 打斷點(diǎn)調(diào)試,甚至加密方式都是一樣,不斷的打斷點(diǎn),最后發(fā)現(xiàn)d是這樣的
{"hlpretag":"","hlposttag":"","s":"許嵩","type":"1","offset":"0","total":"true","limit":"30","csrf_token":""}
我們更改一下s的值, 歌曲名稱 歌手 都可以, 構(gòu)建好這個(gè)字典, 發(fā)送這個(gè)網(wǎng)址,就可以得到id了, 然后拿著id去繼續(xù)構(gòu)造上面的d值, 就可以拿到歌曲url了。
如下:
# -*- coding : utf-8 -*- # @Time : 2020/9/17 14:59 # @author : 沙漏在下雨# @Software : PyCharm# @CSDN : https://me.csdn.net/qq_45906219from GetParams import GetParamsimport requestsfrom get_useragent import GetUserAgentCSimport randomimport keyringimport yagmailclass DownMp3(object): def __init__(self): self.GetIdUrl = "https://music.163.com/weapi/cloudsearch/get/web?csrf_token=" self.GetMP3Url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=' self.ua = GetUserAgentCS().get_user() self.headers = {"User-Agent": self.ua} self.MUSIC_LIST = [] # The singer music demo list self.Sented_qq_email = self.get_email() def get_email(self): email_list = input("輸入QQ郵箱 如果你有多個(gè) 請用空格隔開:").split() if len(email_list) == 1: if "@qq.com" not in email_list[0]: raise Exception("郵箱規(guī)格好像不合適,你輸入的是 ", email_list[0]) else: return email_list[0] elif len(email_list) >= 2: for i in email_list: if "@qq.com" not in i: raise Exception("郵箱規(guī)格好像不合適,你輸入的是 ", i) else: pass return email_list def my_request(self, url, model="get", params=None): if model == 'post': r = requests.post(url, headers=self.headers, params=params) if r.status_code == 200: r.encoding = r.apparent_encoding s = r.json() return s elif model == 'get': r = requests.get(url, headers=self.headers, params=params) if r.status_code == 200: return r.content else: raise Exception("method is error !") def get_mp3_id_demo(self, start=None): """ get the mp3 id {"hlpretag":"<span class=\"s-fc7\">","hlposttag":"</span>","s":"本兮","type":"1","offset":"0","total":"true","limit":"30","csrf_token":""} """ if start is None: raise Exception("You should enter a start name, but you enter start =", start) d = { "hlpretag": "<span class=\"s-fc7\">", "hlposttag": "</span>", "s": str(start), "type": "1", "offset": "0", "total": "true", "limit": "30", "csrf_token": "" } params = GetParams().get_encrypt_params(str(d)) return self.my_request(self.GetIdUrl, model="post", params=params)["result"]["songs"] def get_mp3_url(self, id): """ params: id the music of id fix the id into "{"ids":"[35440198]","level":"standard","encodeType":"aac","csrf_token":""}" so we can get the music the downpath """ d = {"ids": str([id]), "level": "standard", "encodeType": "aac", "csrf_token": ""} params = GetParams().get_encrypt_params(str(d)) context = self.my_request(self.GetMP3Url, model="post", params=params) mp3_path_url = context.get("data")[0]["url"] return mp3_path_url def print_id_list(self, id_list): """ params: id_list print the singer about 30s musics """ a = {} for index, value in enumerate(id_list): a['count'] = (index + 1) a["singer_name"] = value.get("name") a["id"] = value.get("id") a["album"] = value.get("al").get("name") a["image"] = value.get("al").get("picUrl") self.MUSIC_LIST.append(a.copy()) def random_get_mp3(self): mp3Ten = random.sample(self.MUSIC_LIST, 10) # 提出十首歌 content = "" # 把數(shù)據(jù)寫入html中 方便發(fā)送 content += '<p><font size="20" color="Tan">Happy day for you !</font></p>' content += '<table border="1" style="border-collapse: collapse;">\n<caption>Today music demo </caption>\n<tr><th>序號</th><th>歌曲名</th><th>歌曲鏈接</th><th>歌曲所屬</th><th>美圖</th></tr>' count = 1 for j in mp3Ten: s = f"\n<tr><th>{count}</th><th>{j['singer_name']}</th>" \ f"<th><a href='{self.get_mp3_url(j['id'])}'>點(diǎn)擊播放</a></th><th>{j['album']}</th>" \ f"<th><img src='{j['image']}' alt='美圖' height='400' width='400' /></th></tr>" content += s count += 1 content += "</table>" return content def sent_email(self, content): """ sent the music demo list for you like one """ pwd = keyring.get_password("qqemail", "884427640") yag = yagmail.SMTP("884427640@qq.com", pwd, host="smtp.qq.com") # test qq 2817634007@qq.com yag.send(self.Sented_qq_email, '網(wǎng)易云專屬推送', content) yag.close() print("Today music already sent ok!") def start_demo(self): try: start_name = input("input a music singer or music name " "if you like it:") id_list = self.get_mp3_id_demo(start=start_name) self.print_id_list(id_list) print(self.MUSIC_LIST) self.sent_email(self.random_get_mp3()) except Exception as e: print("出現(xiàn)error", e, "再試一次!") self.start_demo()# 如果要運(yùn)行此程序 請打開下面的注釋# a = DownMp3()# a.start_demo()
函數(shù)庫用到
import keyring
import yagmail
下載一下就可以了,
關(guān)于keyring 這是一個(gè)保存密碼的庫, 對于一些密碼來說,我們可以這樣
keyring set qq 88442764然后就會讓你輸入密碼 ,當(dāng)你輸入要獲得就這樣keyring get qq 884427640 就可以了前提你的keyring.exe 在環(huán)境變量中, 當(dāng)然在python中,這個(gè)也是很簡單使用的。
關(guān)于yagmail 這是一個(gè)發(fā)送郵箱的函數(shù)庫
def sent_email(self, content): """ sent the music demo list for you like one """ pwd = keyring.get_password("qqemail", "884427640") yag = yagmail.SMTP("884427640@qq.com", pwd, host="smtp.qq.com") # test qq 2817634007@qq.com yag.send(self.Sented_qq_email, '網(wǎng)易云專屬推送', content) yag.close() print("Today music already sent ok!")
pwd 這個(gè)是郵箱的QQ郵箱的授權(quán)碼, 很長的字符串,要去QQ郵箱里面開啟服務(wù),所以我就放到密碼庫里面了,然后用SMTP鏈接一下郵箱, 就這樣發(fā)送就可以了。
懂點(diǎn)html的都應(yīng)該會編寫這個(gè)。
就這樣寫一下就可以了。 發(fā)送全部代碼:# -*- coding : utf-8 -*- # @Time : 2020/9/17 14:59 # @author : 沙漏在下雨# @Software : PyCharm# @CSDN : https://me.csdn.net/qq_45906219from GetParams import GetParamsimport requestsfrom get_useragent import GetUserAgentCSimport randomimport keyringimport yagmailclass DownMp3(object): def __init__(self): self.GetIdUrl = "https://music.163.com/weapi/cloudsearch/get/web?csrf_token=" self.GetMP3Url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=' self.ua = GetUserAgentCS().get_user() self.headers = {"User-Agent": self.ua} self.MUSIC_LIST = [] # The singer music demo list self.Sented_qq_email = self.get_email() def get_email(self): email_list = input("輸入QQ郵箱 如果你有多個(gè) 請用空格隔開:").split() if len(email_list) == 1: if "@qq.com" not in email_list[0]: raise Exception("郵箱規(guī)格好像不合適,你輸入的是 ", email_list[0]) else: return email_list[0] elif len(email_list) >= 2: for i in email_list: if "@qq.com" not in i: raise Exception("郵箱規(guī)格好像不合適,你輸入的是 ", i) else: pass return email_list def my_request(self, url, model="get", params=None): if model == 'post': r = requests.post(url, headers=self.headers, params=params) if r.status_code == 200: r.encoding = r.apparent_encoding s = r.json() return s elif model == 'get': r = requests.get(url, headers=self.headers, params=params) if r.status_code == 200: return r.content else: raise Exception("method is error !") def get_mp3_id_demo(self, start=None): """ get the mp3 id {"hlpretag":"<span class=\"s-fc7\">","hlposttag":"</span>","s":"本兮","type":"1","offset":"0","total":"true","limit":"30","csrf_token":""} """ if start is None: raise Exception("You should enter a start name, but you enter start =", start) d = { "hlpretag": "<span class=\"s-fc7\">", "hlposttag": "</span>", "s": str(start), "type": "1", "offset": "0", "total": "true", "limit": "30", "csrf_token": "" } params = GetParams().get_encrypt_params(str(d)) return self.my_request(self.GetIdUrl, model="post", params=params)["result"]["songs"] def get_mp3_url(self, id): """ params: id the music of id fix the id into "{"ids":"[35440198]","level":"standard","encodeType":"aac","csrf_token":""}" so we can get the music the downpath """ d = {"ids": str([id]), "level": "standard", "encodeType": "aac", "csrf_token": ""} params = GetParams().get_encrypt_params(str(d)) context = self.my_request(self.GetMP3Url, model="post", params=params) mp3_path_url = context.get("data")[0]["url"] return mp3_path_url def print_id_list(self, id_list): """ params: id_list print the singer about 30s musics """ a = {} for index, value in enumerate(id_list): a['count'] = (index + 1) a["singer_name"] = value.get("name") a["id"] = value.get("id") a["album"] = value.get("al").get("name") a["image"] = value.get("al").get("picUrl") self.MUSIC_LIST.append(a.copy()) def random_get_mp3(self): mp3Ten = random.sample(self.MUSIC_LIST, 10) # 提出十首歌 content = "" # 把數(shù)據(jù)寫入html中 方便發(fā)送 content += '<p><font size="20" color="Tan">Happy day for you !</font></p>' content += '<table border="1" style="border-collapse: collapse;">\n<caption>Today music demo </caption>\n<tr><th>序號</th><th>歌曲名</th><th>歌曲鏈接</th><th>歌曲所屬</th><th>美圖</th></tr>' count = 1 for j in mp3Ten: s = f"\n<tr><th>{count}</th><th>{j['singer_name']}</th>" \ f"<th><a href='{self.get_mp3_url(j['id'])}'>點(diǎn)擊播放</a></th><th>{j['album']}</th>" \ f"<th><img src='{j['image']}' alt='美圖' height='400' width='400' /></th></tr>" content += s count += 1 content += "</table>" return content def sent_email(self, content): """ sent the music demo list for you like one """ pwd = keyring.get_password("qqemail", "884427640") yag = yagmail.SMTP("884427640@qq.com", pwd, host="smtp.qq.com") # test qq 2817634007@qq.com yag.send(self.Sented_qq_email, '網(wǎng)易云專屬推送', content) yag.close() print("Today music already sent ok!") def start_demo(self): try: start_name = input("input a music singer or music name " "if you like it:") id_list = self.get_mp3_id_demo(start=start_name) self.print_id_list(id_list) print(self.MUSIC_LIST) self.sent_email(self.random_get_mp3()) except Exception as e: print("出現(xiàn)error", e, "再試一次!") self.start_demo()# 如果要運(yùn)行此程序 請打開下面的注釋# a = DownMp3()# a.start_demo() def random_get_mp3(self): mp3Ten = random.sample(self.MUSIC_LIST, 10) # 提出十首歌 content = "" # 把數(shù)據(jù)寫入html中 方便發(fā)送 content += '<p><font size="20" color="Tan">Happy day for you !</font></p>' content += '<table border="1" style="border-collapse: collapse;">\n<caption>Today music demo </caption>\n<tr><th>序號</th><th>歌曲名</th><th>歌曲鏈接</th><th>歌曲所屬</th><th>美圖</th></tr>' count = 1 for j in mp3Ten: s = f"\n<tr><th>{count}</th><th>{j['singer_name']}</th>" \ f"<th><a href='{self.get_mp3_url(j['id'])}'>點(diǎn)擊播放</a></th><th>{j['album']}</th>" \ f"<th><img src='{j['image']}' alt='美圖' height='400' width='400' /></th></tr>" content += s count += 1 content += "</table>" return content
就這樣寫一下就可以了。
# -*- coding : utf-8 -*- # @Time : 2020/9/17 14:59 # @author : 沙漏在下雨# @Software : PyCharm# @CSDN : https://me.csdn.net/qq_45906219from GetParams import GetParamsimport requestsfrom get_useragent import GetUserAgentCSimport randomimport keyringimport yagmailclass DownMp3(object): def __init__(self): self.GetIdUrl = "https://music.163.com/weapi/cloudsearch/get/web?csrf_token=" self.GetMP3Url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=' self.ua = GetUserAgentCS().get_user() self.headers = {"User-Agent": self.ua} self.MUSIC_LIST = [] # The singer music demo list self.Sented_qq_email = self.get_email() def get_email(self): email_list = input("輸入QQ郵箱 如果你有多個(gè) 請用空格隔開:").split() if len(email_list) == 1: if "@qq.com" not in email_list[0]: raise Exception("郵箱規(guī)格好像不合適,你輸入的是 ", email_list[0]) else: return email_list[0] elif len(email_list) >= 2: for i in email_list: if "@qq.com" not in i: raise Exception("郵箱規(guī)格好像不合適,你輸入的是 ", i) else: pass return email_list def my_request(self, url, model="get", params=None): if model == 'post': r = requests.post(url, headers=self.headers, params=params) if r.status_code == 200: r.encoding = r.apparent_encoding s = r.json() return s elif model == 'get': r = requests.get(url, headers=self.headers, params=params) if r.status_code == 200: return r.content else: raise Exception("method is error !") def get_mp3_id_demo(self, start=None): """ get the mp3 id {"hlpretag":"<span class=\"s-fc7\">","hlposttag":"</span>","s":"本兮","type":"1","offset":"0","total":"true","limit":"30","csrf_token":""} """ if start is None: raise Exception("You should enter a start name, but you enter start =", start) d = { "hlpretag": "<span class=\"s-fc7\">", "hlposttag": "</span>", "s": str(start), "type": "1", "offset": "0", "total": "true", "limit": "30", "csrf_token": "" } params = GetParams().get_encrypt_params(str(d)) return self.my_request(self.GetIdUrl, model="post", params=params)["result"]["songs"] def get_mp3_url(self, id): """ params: id the music of id fix the id into "{"ids":"[35440198]","level":"standard","encodeType":"aac","csrf_token":""}" so we can get the music the downpath """ d = {"ids": str([id]), "level": "standard", "encodeType": "aac", "csrf_token": ""} params = GetParams().get_encrypt_params(str(d)) context = self.my_request(self.GetMP3Url, model="post", params=params) mp3_path_url = context.get("data")[0]["url"] return mp3_path_url def print_id_list(self, id_list): """ params: id_list print the singer about 30s musics """ a = {} for index, value in enumerate(id_list): a['count'] = (index + 1) a["singer_name"] = value.get("name") a["id"] = value.get("id") a["album"] = value.get("al").get("name") a["image"] = value.get("al").get("picUrl") self.MUSIC_LIST.append(a.copy()) def random_get_mp3(self): mp3Ten = random.sample(self.MUSIC_LIST, 10) # 提出十首歌 content = "" # 把數(shù)據(jù)寫入html中 方便發(fā)送 content += '<p><font size="20" color="Tan">Happy day for you !</font></p>' content += '<table border="1" style="border-collapse: collapse;">\n<caption>Today music demo </caption>\n<tr><th>序號</th><th>歌曲名</th><th>歌曲鏈接</th><th>歌曲所屬</th><th>美圖</th></tr>' count = 1 for j in mp3Ten: s = f"\n<tr><th>{count}</th><th>{j['singer_name']}</th>" \ f"<th><a href='{self.get_mp3_url(j['id'])}'>點(diǎn)擊播放</a></th><th>{j['album']}</th>" \ f"<th><img src='{j['image']}' alt='美圖' height='400' width='400' /></th></tr>" content += s count += 1 content += "</table>" return content def sent_email(self, content): """ sent the music demo list for you like one """ pwd = keyring.get_password("qqemail", "884427640") yag = yagmail.SMTP("884427640@qq.com", pwd, host="smtp.qq.com") # test qq 2817634007@qq.com yag.send(self.Sented_qq_email, '網(wǎng)易云專屬推送', content) yag.close() print("Today music already sent ok!") def start_demo(self): try: start_name = input("input a music singer or music name " "if you like it:") id_list = self.get_mp3_id_demo(start=start_name) self.print_id_list(id_list) print(self.MUSIC_LIST) self.sent_email(self.random_get_mp3()) except Exception as e: print("出現(xiàn)error", e, "再試一次!") self.start_demo()# 如果要運(yùn)行此程序 請打開下面的注釋# a = DownMp3()# a.start_demo()
擴(kuò)展了一個(gè)下載一首歌的代碼,如果你需要。
# -*- coding : utf-8 -*- # @Time : 2020/9/17 21:35 # @author : 沙漏在下雨# @Software : PyCharm# @CSDN : https://me.csdn.net/qq_45906219import requestsfrom get_useragent import GetUserAgentCSfrom GetParams import GetParamsfrom DownMp3 import DownMp3class DownOneMp3(DownMp3): def __init__(self): super().__init__() self.GetIdUrl = "https://music.163.com/weapi/cloudsearch/get/web?csrf_token=" self.params_url = "https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=" self.headers = {"user-agent": GetUserAgentCS().get_user()} self.start = input("Please input the music name:") ids = self.get_id() self.params = self.get_params(id=ids) self.mp3_name = self.start + ".mp3" def my_request(self, url, model="get", params=None): """ 繼承父類的一個(gè)方法 """ return super().my_request(url, model, params) def get_params(self, id): """給出id 返回加密參數(shù)""" d = {"ids": str([id]), "level": "standard", "encodeType": "aac", "csrf_token": ""} params = GetParams().get_encrypt_params(str(d)) return params def get_id(self): """ 根據(jù)歌曲名稱獲取id """ start = self.start d = { "hlpretag": "<span class=\"s-fc7\">", "hlposttag": "</span>", "s": str(start), "type": "1", "offset": "0", "total": "true", "limit": "30", "csrf_token": "" } params = GetParams().get_encrypt_params(str(d)) return self.my_request(self.GetIdUrl, model="post", params=params)["result"]["songs"][0].get("id") def spider(self): """ 這是爬取一首歌的方式, 你只需要輸入歌曲名稱就可以 會自動(dòng)調(diào)用其他類實(shí)現(xiàn)參數(shù)加密 id獲取等 """ import os r = requests.post(self.params_url, params=self.params) if r.status_code == 200: print(r.json()) mp3 = r.json().get("data")[0].get("url") print("music link is ", mp3) rmp3 = requests.get(mp3, headers=self.headers) if rmp3.status_code == 200: with open(self.mp3_name, 'wb') as ;fw: ;fw.write(rmp3.content) print("Down Successful! ", "file path is ", os.path.dirname(__file__)) # 如果要運(yùn)行此程序 請打開下面的注釋# a = DownOneMp3()# a.spider()
""" 如果你僅僅只是想下載一首歌 跳轉(zhuǎn)到DownOneMp3模塊啟動(dòng)模塊運(yùn)行 如果你想多首歌發(fā)送某人郵箱 跳轉(zhuǎn)到DownMp3模塊啟動(dòng)模塊運(yùn)行 """
到此,關(guān)于“如何使用JS逆向方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。