虾米音乐 音乐下载爬虫 参数逆向过程 附源码
https://www.kxdao.net/forum.php?mod=image&aid=434552&size=300x300&key=9f61483b8651d85a&nocache=yes&type=fixnonehttps://www.kxdao.net/forum.php?mod=image&aid=434551&size=300x300&key=30596883d439e955&nocache=yes&type=fixnone
https://www.kxdao.net/forum.php?mod=image&aid=434549&size=300x300&key=db4ab1abf078e289&nocache=yes&type=fixnone
https://www.kxdao.net/forum.php?mod=image&aid=434550&size=300x300&key=ac99db6d2bf2e303&nocache=yes&type=fixnone
https://www.kxdao.vip/data/attachment/forum/202101/06/154124p53421z40f1fnopo.png
https://www.kxdao.vip/data/attachment/forum/202101/06/154124nkwkxtb6eky55h6w.jpg
https://www.kxdao.vip/data/attachment/forum/202101/06/154125v40ox8zmfd89h94x.png
https://www.kxdao.vip/data/attachment/forum/202101/06/154125ldfaygy06gqfad0h.png
https://www.kxdao.vip/data/attachment/forum/202101/06/154125rzxn9ilm7zb5a2lb.png
虾米音乐即将关闭,闲来无事研究了下其音乐下载的参数加密,并用python实现了一部分解密过程
python代码
[*]import os
import requests
from lxml import etree
import re
import json
import hashlib
cookie = input('输入口令:')
serch = input('输入搜索关键字:')
headers = {
'cookie': cookie,
}
i = 1
while 1:
print(f'第{i}页')
url = 'https://www.xiami.com/list?page=' + str(i) + '&query={"searchKey":"' + serch + '"}&scene=search&type=song'
response_index = requests.get(url)
html = etree.HTML(response_index.text)
trs = html.xpath('//tbody/tr')
for tr in trs:
song_name = tr.xpath('.//div[@class="song-name em"]//span/text()')
song_album = ''.join(tr.xpath('.//div[@class="album"]//span//text()'))
song_url = 'https://www.xiami.com' + tr.xpath('.//div[@class="song-name em"]/a/@href')
response = requests.get(song_url)
song_id = re.findall(r'content="https://www.xiami.com/song/([\s\S]*?)">', response.text)
s = hashlib.md5((headers['cookie'].split('=').split('_') +
'_xmMain_/api/song/getPlayInfo_{"songIds":[' +
str(song_id) + ']}').encode('utf-8')).hexdigest().lower()
song_down_url = 'https://www.xiami.com/api/song/getPlayInfo?_q=%7B%22songIds%22:[{}]%7D&_s={}'.format(song_id,
s)
response_down = requests.get(song_down_url, headers=headers)
json_data = json.loads(response_down.text)
try:
song_download = json_data['result']['data']['songPlayInfos']['playInfos']['listenFile'] if \
json_data['result']['data']['songPlayInfos']['playInfos']['listenFile'] else \
json_data['result']['data']['songPlayInfos']['playInfos']['listenFile']
dirs = './music'
if not os.path.exists(dirs):
os.makedirs(dirs)
f = open(r"./music/{}.mp3".format((song_name + '-' + song_album).replace('\\', '')), "wb+")
f.write(requests.get(song_download).content)
f.close()
print(song_name, song_album, '下载完成')
except Exception as e:
if 'No schema supplied' in str(e):
print(song_name, song_album, '虾米没有版权 没有下载地址')
else:
print(e)
continue
if 'true' in html.xpath('//li[@title="下一页"]/@aria-disabled'):
print(f'一共{i}页都爬取完成 结束')
break
i += 1
复制代码
[附件]154124p53421z40f1fnopo.png(下载失败)
[附件]154124nkwkxtb6eky55h6w.jpg(下载失败)
[附件]154125v40ox8zmfd89h94x.png(下载失败)
[附件]154125ldfaygy06gqfad0h.png(下载失败)
[附件]154125rzxn9ilm7zb5a2lb.png(下载失败)
这画风一下就想起以前了
页:
[1]