from lxml import etree
import requests
import os
from urllib import request,parse
import json
import execjs
def spider_songs(list):
'''保存音频(字典)'''
for i in list:
dir = "ximalaya/{}/".format(i['bookName'])
if not os.path.exists(dir):
print("创建目录:.%s" % dir)
os.makedirs(dir)
i['name'] = i['name'].replace("?", "").replace('"', "")
# 在目录下创建一个喜马拉雅的文件夹
with open(r'{}/{}.m4a'.format(dir, i['name']), 'ab')as f:
r = requests.get(i['src'])
print("正在下载:{}...".format(i['name']), end="")
f.write(r.content)
print("\t下载完成!")
# def spider_song(m4a_url,m4a_name):
# # 创建down文件夹
# if not os.path.exists('Down'):
# os.mkdir('Down')
# filename = 'Down/' + m4a_name + '.m4a'
# # print(filename)
# # 下载
# request.urlretrieve(m4a_url, filename)
# print(m4a_url + m4a_name + 'downloading...')
# print('---' * 50)
def spider_list(albumId):
all_list = []
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
'Accept':'text/html,application/xhtml+ xml,application/xml;q = 0.9,image/webp,image/apng,*/*;q=0.8, application/signe-exchange;v = b3',
'Host': 'www.ximalaya.com'
}
sign = exec_js()
#加入xm-sign到header中
headers['xm-sign'] = sign
# 音频地址
data_url = 'https://www.ximalaya.com/revision/play/album?albumId={}&pageNum=1&sort=-1&pageSize=30'.format(albumId)
# print(data_url)
response = requests.get(data_url, headers=headers)
py_dict = json.loads(response.content.decode())
# print(py_dict)
book_list = py_dict['data']['tracksAudioPlay']
for book in book_list:
# 获取每段音频的名称和地址
list = {}
list['name'] = book['trackName']
list['bookName'] = book['albumName']
list['src'] = book['src']
# 打印list
# print(list)
all_list.append(list)
# 返回字典
return all_list
'''生成xm-sign'''
def exec_js():
#获取喜马拉雅系统时间戳
time = getxmtime()
#读取同一路径下的js文件
with open('xmSign.js', encoding='utf-8') as f:
js = f.read()
# 通过compile命令转成一个js对象
docjs = execjs.compile(js)
# 调用js的function
res = docjs.call('python',time)
#res = docjs.call('getnow')
return res
'''爬取喜马拉雅服务器系统时间戳,用于生成xm-sign'''
def getxmtime():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
'Accept': 'text/html,application/xhtml+ xml,application/xml;q = 0.9,image/webp,image/apng,*/*;q=0.8, application/signe-exchange;v = b3',
'Host': 'www.ximalaya.com'
}
url="https://www.ximalaya.com/revision/time"
response = requests.get(url, headers=headers)
# print(response)
html = response.text
return html
def spider_ximalaya(albumId):
all_list = spider_list(albumId)
spider_songs(all_list)
if __name__ =='__main__':
spider_ximalaya(12642314)