最近因为无聊,又想听歌,就看了下论坛大神的帖子,然后自己写了一遍,粗糙的脚本,初学者应该也能看的懂(毕竟我也是初学的),哈哈
## 哈哈,希望有大佬可以把界面写出来,如有违规,请删除
这里我用Markdown 编辑的时候显示是白色背景,不知道发出去之后会不会变成黑色,白色背景下面有些字符显示不到,如果看到的话,可以选择代码去看,,,emmmm 还是循例说一句,此代码仅供学习使用
废话不多说,上代码
#!/usr/bin/env python
import requests
import json
from re import sub
class MoreSound(object):
# 初始化请求头,好像要检测cookies,cookies貌似也不过期,哈哈
def __init__(self):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'cookie': 'XLA_CI=524d712cbc042638e82facf37fac93e4; IP=cBUpkdzEhlphEhKRGYnYs79LiFtojigjY%252Fy1spZXf4M%253B; QQMUSIC_SIP=%5B%22http%3A//122.5.0.155/amobile.music.tc.qq.com/%22%2C%22http%3A//122.5.0.156/amobile.music.tc.qq.com/%22%2C%22http%3A//122.5.0.144/amobile.music.tc.qq.com/%22%5D; QQ=',
}
self.headers = headers
#搜索歌曲并返回 歌名 以及 歌手列表
def get_list(self, s_name):
id_list = []
name_list = []
n = 1
url = 'https://moresound.tk/music/api.php?search=mg'
data = {
'w': s_name,
'p': '1',
'n': '15',
}
res = requests.post(url=url, headers=self.headers, data=data)
res = json.loads(res.text)
info = res['song_list']
# print(info)
print('编号', '\t', '歌名---' , '\t','---歌手')
for k in info:
song_name = k['songname']
song_name = sub('<sup.*|\n|\r|', '', str(song_name)).replace(' ', '')
song_id = k['songmid']
singer = k['singer'][0]['name']
id_list.append(song_id)
name_list.append(song_name)
print(n, '\t', song_name+'---'+ singer)
n += 1
return id_list, name_list
#得到的歌曲信息
def get_info(self, id):
n = 1
type_list = []
data = {'mid': id, }
url = 'https://moresound.tk/music/api.php?get_song=mg'
res = requests.post(url=url, data=data, headers=self.headers)
res = json.loads(res.text)
url_info = res['url']
print('编号', '\t', '格式')
for key, val in zip(url_info.keys(), url_info.values()):
type_list.append(val)
print(n, '\t', key)
n += 1
return type_list
# 获取真正的url 并下载保存
def get_real_url(self, type_id, name):
url = 'https://moresound.tk/music/'
url = url + type_id
res = requests.get(url=url, headers=self.headers)
res = json.loads(res.text)
try:
ty = res['suffix']
real_url = res['url']
r = requests.get(url=real_url, headers=self.headers, stream=False)
#此处保存在F:\music文件夹,没有请新建
with open('F:\\music\\{0}.{1}'.format(name, ty), 'wb') as f:
f.write(r.content)
# for chunk in r.iter_content():
# f.write(chunk)
except Exception as e:
print('暂无资源,请重新选择!')
print('done!')
if __name__ == '__main__':
while True:
c = input('请输入歌名或者歌手名字,回车确认,输入Q退出!:')
if c.lower() =='q':
break
a = MoreSound()
id_list, name_list = a.get_list(c)
# print(id_list)
s_id = input('请输入需要下载的歌曲编号,回车确认:')
id = id_list[int(s_id) - 1]
name = name_list[int(s_id) - 1]
type_list = a.get_info(id)
t_id = input('请输入需要下载的格式,回车确认:')
type_id = type_list[int(t_id) - 1]
b = a.get_real_url(type_id, name)