import requests
import re
import json
import os def get_name(singer):
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
params = {
'catZhida': '',
'w': singer,
}
headers = {
'referer': 'https://y.qq.com/portal/search.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
html = requests.get(url,headers=headers,params=params).text
content = re.compile('callback\((.*)\)').findall(html)[0]
content = json.loads(content)
data = content.get('data')
song = data.get('song')
lists = song.get('list')
name = []
for list in lists:
singer = list.get('singer')[0].get('mid')
name.append(singer)
name = name[0]
return name def get_html(name,singer):
url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg'
params = {
'singermid': name,
'order': 'listen',
'begin': '',
'num': '',
}
headers = {
'referer': 'https://y.qq.com/n/yqq/singer/003aQYLo2x8izP.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
html = requests.get(url,headers=headers,params=params).text
return html def get_music(vkey,songname,filename,singer):
if vkey and songname:
url3 = 'http://dl.stream.qqmusic.qq.com/' + filename + '?vkey=' + vkey + '&guid=7133372870&uin=0&fromtag=66' headers = {
'referer': 'https://y.qq.com/n/yqq/singer/003aQYLo2x8izP.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
music = requests.get(url3,headers=headers).content
dir = singer
if not os.path.exists(dir):
os.mkdir(dir)
with open(dir+'/'+songname+'.m4a','wb') as f:
f.write(music)
print(songname,'__',singer) def get_vkey(strMediaMid,songmid,songname,singer):
if strMediaMid and songmid and songname :
url2 = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg'
params = {
'g_tk': '',
'jsonpCallback': 'MusicJsonCallback8571665793949388',
'loginUin': '',
'hostUin': '',
'format': 'json',
'inCharset': 'utf8',
'outCharset': 'utf-8',
'notice': '',
'platform': 'yqq',
'needNewCode': '',
'cid': '',
'callback': 'MusicJsonCallback8571665793949388',
'uin': '',
'songmid': songmid,
'filename': 'C400'+ strMediaMid + '.m4a',
'guid': ''
}
headers = {
'referer': 'https://y.qq.com/portal/player.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
detail_html = requests.get(url2,headers=headers,params=params).text
vkey_disc = re.compile('MusicJsonCallback8571665793949388\((.*?)\)').findall(detail_html)[0]
vkey_disc = json.loads(vkey_disc) data = vkey_disc['data']
items = data.get('items')[0]
vkey = items.get('vkey')
get_music(vkey,songname,'C400'+ strMediaMid + '.m4a',singer) def get_list(detail_html,singer):
if detail_html:
lists = re.compile('data\":{\"list\":(.*?),\"singer_id',re.S).findall(detail_html)[0]
datas = json.loads(lists)
for data in datas:
musicData = data.get('musicData')
strMediaMid = musicData.get('strMediaMid')
songmid = musicData.get('songmid')
songname = musicData.get('songname')
get_vkey(strMediaMid,songmid,songname,singer) def main():
singer = input('请输入您想要下载的歌手:')
name = get_name(singer)
detail_html = get_html(name,singer)
get_list(detail_html,singer) if __name__ == '__main__':
main()

有些地方代码有些冗余,还可以再改进  但是费了些功夫终于爬出的效果 确实想要快点编辑出来  按耐不住小激动  这应该就是敲代码的乐趣

python3 爬去QQ音乐-LMLPHP

当播放一个音乐的时候  在Elements中 可以看到音乐的链接  当然是通过js 和 css 加载过的   但是 可以用逆向思维进行参数的找寻

http://dl.stream.qqmusic.qq.com/C4000032qfIn1Tzdbq.m4a?vkey=80525033DD719DAB87C0CEC7B4F9F40D8755982D3A495E3BA0810E50A89668A2AFD61C4C19876FD0FB68932DED89B340AE9F4F464D82DC15&guid=7133372870&uin=0&fromtag=66

发现  vkey 是一个很重要的参数  所以就先去翻一翻网页查一下vkey在哪

python3 爬去QQ音乐-LMLPHP

经发现 在同播放页面的JS中   但是如果想获得vkey 就需要访问这个对应的URL  也要找到相应的参数

python3 爬去QQ音乐-LMLPHP

标红箭头的是一直在变化而且没有不行的参数

所以将继续往里使劲挖!

python3 爬去QQ音乐-LMLPHP

目前处于列表页  发现参数已经浮现

但发现第一个红箭头是歌手的意思  被不知道用了什么样的格式换了一种形式  因为后续还想通过段小乱码找到每个歌手所对应的歌曲 所以 还是找到每个歌手所对应的小乱码比较好

python3 爬去QQ音乐-LMLPHP

找到了!!!

但是 写代码的话 要用正向思维来写  Year!

05-08 14:59