虽然这段代码不难, 但还是有人不自己写却把我写的代码放到网上来, 整理了一翻, 自己放一次
import json
import os
import re
import requests
def xima():
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
}
url_1 = "https://www.ximalaya.com/revision/getRankCluster"
json_1 = requests.get(url_1, headers=headers).content.decode()
dict_1 = json.loads(json_1)
name_1 = dict_1['data']['list']
for i in name_1:
name = i['name']
print(name)
url_2 = "https://www.ximalaya.com/revision/getRankList?code=" + name
json_2 = requests.get(url_2, headers=headers).content.decode()
dict_2 = json.loads(json_2)
for j in dict_2['data']['albums']:
id_2 = j['id']
title = j['albumTitle']
title = re.sub('\?|"|\|', '', title)
if not os.path.exists(title):
os.mkdir(title)
url_3 = "https://www.ximalaya.com/revision/play/album?albumId=" + str(
id_2) + "&pageNum={}&sort=-1&pageSize=30"
for i in range(50):
url = url_3.format(i + 1)
try:
json_3 = requests.get(url, headers=headers).content.decode()
except Exception as e:
print(e)
continue
dict_3 = json.loads(json_3)
n = 1
for k in dict_3['data']['tracksAudioPlay']:
src = k['src']
name = k['trackName']
print(src, name)
name = re.sub('\?|"|\|', '', name)
with open(title + '/' + str(i * 30 + n) + '%s.m4a' % name, 'ab') as f:
r = requests.get(src, headers=headers)
ret = r.content
f.write(ret)
n += 1
if __name__ == '__main__':
xima()