最近点赞了一些文章,觉得有些东西是我还没有接触过的,于是打算复刻一次,但是用我的思路,加上一些新的想法,最后我也是成功实现了这些功能。
就和标题一样,这次打算解析一下 网易云 请求的参数,然后把爬来的歌曲编写到前端html代码里面,最后用代码实现QQ邮箱的发送功能,嗯,没错,确实创新了一点,但是在编写代码的框架思路上, 每个人都是有所不同, 找到自己的方法就可以了。
我本以为用Python实现了它的加密算法之后,可以解析(very import person) 歌曲, 但是根据我的实际操作上,是不可以的,后面也会讲到,他们加密的参数是不一样的,都有各自的播放渠道, 如果你熟悉 html 你都可以发现他有二个播放渠道, 分别对应一般和特殊,所以后面我直接就开了黑胶会员, 我就想知道参数到底有什么不同, 不幸的是,当我在网页端登入账号的时候, 我发现在我打断点之后,我就不能启动调试功能了,相反的是 debugger parse 这样的字段,我尝试去解决这个问题,根据网上各种方式,到后面还是无济于事,索性放弃了,但是对于一般的 music , 是完全没有问题的,都可以下载,发送,保存到本地。
后面当我把这代码全部写好,加上了一些异常处理,我想到我的憨憨女友,索性在加一点代码实现,就把这些爬来的数据 写道一个 /<table> 里面, 然后再用其他函数库发送到她邮箱去了,单纯觉得好玩。
这篇文章有点长,因为有点难懂, 我只是想讲清楚一点,给她看,或者 给一些基础不是很好的人看一下, 大佬可以亲喷点,代码用到了很多库,但是我再后面也会一个个提到,不影响各位阅读, 只要耐心的看,你就一定有收获。
文章三天之后设置为粉丝可见。
歌曲版权最终归网易云所有!
JS逆向, ASE加密, RES非对称算法,yagmail邮箱的发送,浏览器的debug,学到就是爽。
就是一个 html 表格, 熟悉的应该就直接能看出来, 然后还有img标签, a标签之类的。
当我们用chrome浏览器 (推荐用谷歌) 进入网易云官网,找到一首你喜欢的歌。
打开 f12 功能, 点击 XHR 过滤, 这个时候,我们点击播放, 在右侧就会重新捕获到新的网络请求,其中就包括我们需要的歌曲文件链接。就像这样。
v1?csrf … 这个网址就是刚刷的, 在响应中可以看到,有个url,你复制打开,就可以直接播放, 我们点击一下headers看看怎么发送的。
请求了request url , 用post发送, 下面有2个参数表单 params 和 encSecKey 貌似我们有下面2个参数就可以直接发送请求了, 所以直接就尝试了一下。
def spider(self):
"""
这是爬取一首歌的方式, 复制params就可以发送请求
"""
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("下载成功")
最后成功下载。
也就是说,我们只需要知道这二个参数怎么来的,就可以自己构造了,那就想怎么就怎么了,这个时候,我们就可以打开浏览器自带的调试功能了。要打断点,要debug, 怎么打,怎么断? 仔细点看我图的注释就可以了。
还是之前的包, 你点击第四个 initiator 就会刷新出很多和他有关系的文件, 我们点击第一个。
然后就来到这样, 还记得之前的二个参数吧, 在这里我们直接 ctrl + f 找其中的一个参数。
这里可以看到 params , encSecKey 都是根据 bvz7s 来的, 而bvz7s 是根据 window.asrsea() 函数来的, 所以在这个 函数打一个断点, 继续看下一个搜索点
在这里,我们发现window.asrsea = d 所以就得看 d 函数,在d函数的语气中,我们都可以打上断点,以便观察清楚。 打上断电之后, 刷新页面,等待一段时间。
之后就到第一个断点处, 然后 f8 跳到下一个断点
然后就可以发现 d 接受的4个参数是什么了, (d, e, f, g) 在右侧我们也可以看到,多次测试发现,后面三个是加密参数,固定值,所以复制拿过来用就可以了, 对于第一个d = {“csrf_token”:"…"} 这个是用来记录你是否登入账号, 如果你没有登陆, 那就是空。
继续f8 跳转到最后
发现就是把最开始接受的4个参数,然后经过a, b, b, c 函数处理就可以了,那待会我们就要看看每个函数有什么作用,这就涉及到他们的加密方式了,但是在这里,就要思考一个问题了,关于最开始的4个参数, 就一个d会变, 其他都没变化, 而且d还是一个空或者乱七八糟的的数字, 那他是怎么知道我是哪一首歌? 哪个歌手,所以这个参数一定有问题, (后面经过加密测试,发现加密后参数长度少了很多) 所以在这里我就继续 调试了一下, 一样的操作。
调试一圈了,最后终于有一个靠谱的了,有歌曲的id 还有歌曲的音质, 其他的,如果不熟悉,可以每一个d都去试试,直到加密参数正确。
所以先确定d为
"{"ids":"[1459950258]","level":"standard","encodeType":"aac","csrf_token":"59098e191e8babbaef83f1b8bbbe5987"}"
姑且就用这个d参数去加密尝试一次吧。
我们只需要一个个了解好,然后用python语言转换一下就可以了。下面分模块讲这些。
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函数接受一个a参数, 然后再一次循环中, 循环一次为a次, 然后从 b中 随机的挑选一些字符, 最后用字符串的形式返回, 对于JAVAscript来说,随机没那么容易,他需要用 random 生成 (0, 1) 的数,然后放大,取整,取值,累加,但对于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()
}
这是一个 AES 加密, 模式 CBC, 其实刚开始我也不知道AES加密是什么东西, 后面我查看了官网文档,参考了其他的办法,实现了。
观察这个函数, 接受了a,b, 其中a,b 是什么可以再函数d中看到到。
根据之前的分析, g是固定值,我们已经复制下来, d 认为是一个字符字典
"{"ids":"[1459950258]","level":"standard","encodeType":"aac","csrf_token":"59098e191e8babbaef83f1b8bbbe5987"}"
这样,我们用python语言加入这些参数,试着模拟一下。
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
这里也是成功实现了,截图我忘记截了。 关于如何AES加密,可以直接看我的,有时间有兴趣的也可以和我一样看官网文档。 都行, 实现就可以了。
function c(a, b, c) {
var d, e;
return setMaxDigits(131),
d = new RSAKeyPair(b,"",c),
e = encryptedString(d, a) }
一看很简单,其实复杂, 用到了RSA加密算法,关于RSA加密算法,我找了一些资料。
大致原理如图:
参考文档
我们用python这样实现;
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() 其实是可以接受三个参数的, 如果有第三个, 第三个就为取余值, 用上int(a, 16) 就可以直接将16进制转换为10进制, 最后的format(num, ‘x’) 将值用16进制形式输出, 然后zfill() 填充131 位数,, (根据函数C得知 位数为131)
分析了上面三个函数, 其实我们就可以直接编写程序加密了, 我们把程序连起来。
# -*- 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): """
这是爬取一首歌的方式, 复制params就可以发送请求
"""
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表单数据的,在这里就要用selenium 去爬取然后分析了,
如果你在下面的情况下,就可以找到id表单数据。
还有一样的,在这个包,我们看到参数还是params 和 encSerKey 然后重复上面操作, 打断点调试,甚至加密方式都是一样,不断的打断点,最后发现d是这样的
{"hlpretag":"<span class="s-fc7">","hlposttag":"</span>","s":"许嵩","type":"1","offset":"0","total":"true","limit":"30","csrf_token":""}
我们更改一下s的值, 歌曲名称 歌手 都可以, 构建好这个字典, 发送这个网址,就可以得到id了, 然后拿着id去继续构造上面的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邮箱 如果你有多个 请用空格隔开:").split()
if len(email_list) == 1:
if "@qq.com" not in email_list[0]:
raise Exception("邮箱规格好像不合适,你输入的是 ", 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("邮箱规格好像不合适,你输入的是 ", 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 = "" # 把数据写入html中 方便发送
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'])}'>点击播放</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, '网易云专属推送', 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("出现error", e, "再试一次!")
self.start_demo()# 如果要运行此程序 请打开下面的注释# a = DownMp3()# a.start_demo()
下载一下就可以了,
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, '网易云专属推送', content)
yag.close()
print("Today music already sent ok!")
pwd 这个是邮箱的QQ邮箱的授权码, 很长的字符串,要去QQ邮箱里面开启服务,所以我就放到密码库里面了,然后用SMTP链接一下邮箱, 就这样发送就可以了。
懂点html的都应该会编写这个。
就这样写一下就可以了。
发送全部代码:# -*- 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邮箱 如果你有多个 请用空格隔开:").split()
if len(email_list) == 1:
if "@qq.com" not in email_list[0]:
raise Exception("邮箱规格好像不合适,你输入的是 ", 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("邮箱规格好像不合适,你输入的是 ", 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 = "" # 把数据写入html中 方便发送
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'])}'>点击播放</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, '网易云专属推送', 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("出现error", e, "再试一次!")
self.start_demo()# 如果要运行此程序 请打开下面的注释# a = DownMp3()# a.start_demo() def random_get_mp3(self): mp3Ten = random.sample(self.MUSIC_LIST, 10) # 提出十首歌
content = "" # 把数据写入html中 方便发送
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'])}'>点击播放</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邮箱 如果你有多个 请用空格隔开:").split()
if len(email_list) == 1:
if "@qq.com" not in email_list[0]:
raise Exception("邮箱规格好像不合适,你输入的是 ", 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("邮箱规格好像不合适,你输入的是 ", 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 = "" # 把数据写入html中 方便发送
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'])}'>点击播放</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, '网易云专属推送', 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("出现error", e, "再试一次!")
self.start_demo()# 如果要运行此程序 请打开下面的注释# a = DownMp3()# a.start_demo()
扩展了一个下载一首歌的代码,如果你需要。
# -*- 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):
"""
继承父类的一个方法
"""
return super().my_request(url, model, params)
def get_params(self, id): """给出id 返回加密参数"""
d = {"ids": str([id]), "level": "standard", "encodeType": "aac", "csrf_token": ""}
params = GetParams().get_encrypt_params(str(d)) return params
def get_id(self): """
根据歌曲名称获取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): """
这是爬取一首歌的方式, 你只需要输入歌曲名称就可以 会自动调用其他类实现参数加密 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__))
# 如果要运行此程序 请打开下面的注释# a = DownOneMp3()# a.spider()
"""
如果你仅仅只是想下载一首歌 跳转到DownOneMp3模块启动模块运行
如果你想多首歌发送某人邮箱 跳转到DownMp3模块启动模块运行
"""
JS逆向, ASE加密, RES非对称算法,yagmail邮箱的发送,浏览器的debug,学到就是爽。
到这里就结束啦。此文转载于:沙漏在下雨,著作权归作者所有,如有侵权联系小编删除!