1. 喜马拉雅音频
1-1 喜马拉雅
import requests
import json
import time
import random
import hashliburl = 'https://www.ximalaya.com/revision/play/v1/audio?id=46103875&ptype=1'headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.47'
}#服务器时间戳
severtime = requests.get('https://www.ximalaya.com/revision/time',headers=headers).text
#现在时间戳
nowtime = str(round(time.time()*1000)) #round()返回保留几位小数的值,默认为整数 //time.time()返回当前时间的时间戳//time.time()得10位数,目前时间戳为13位数#求加密的参数
xm_sign = str(hashlib.md5('himalaya-{}'.format(severtime).encode()).hexdigest()) + '({})'.format(round(random.random()*100)) + severtime + '({})'.format(round(random.random()*100)) + nowtime
#print(xm_sign)#更新请求头参数
headers['xm-sign'] = xm_sign#提取及解析音频网址
resp = requests.get(url,headers=headers)
src_dic = json.loads(resp.text)
src = src_dic['data']['src']#下载音频
with open(f'./video/卖吊票-1.m4a',mode='wb') as f:f.write(requests.get(src,headers=headers).content)
## 再用格式工厂 转mp3
1-2 喜马拉雅
import re
import requests
import re
import requests
import json
import time
import random
import hashlib# 免费
url = 'https://www.ximalaya.com/revision/play/v1/audio?id=46103875&ptype=1'# 收费的还不行
# url = 'https://audiofreepay.ali.xmcdn.com/download/1.0.0/storages/8987-audiopay/37/8B/GKwRIMAH8cjnANYGeAIF8P8Z-aacv2-48K_preview_1231196.m4a'
# url = 'https://www.ximalaya.com/revision/play/v1/audio?id=620471242&ptype=1'headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}# json_url = 'https://www.ximalaya.com/revision/play/v1/audio?id={}&ptype=1'.format(m4a_id)
json_data = requests.get(url, headers=headers).json()#
print(json_data)# 提取音频地址
m4a_url = json_data['data']['src']#
print(m4a_url)# 请求音频数据
m4a_data = requests.get(url=m4a_url, headers=headers).contentnew_title = '卖吊票_岳云鹏郭德纲.m4a'
print(new_title)
# 4.数据持久化(保存)
with open('video\\' + new_title, mode='wb') as f:f.write(m4a_data)
print('保存完成:', new_title)