import requests
# 定义页数
page = 1
# 书籍信息
start_url = 'https://www.ximalaya.com/revision/category/queryCategoryPageAlbums?category=youshengshu&subcategory=yanqing&meta=&sort=0&page={}&perPage=30'
for page in range(30):
url = start_url.format(page)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}
response = requests.get(url, headers=headers).json()
data = response['data']['albums']
for sou in data:
albumId = sou['albumId']
book_url = 'https://www.ximalaya.com/revision/play/v1/show?id={}&num=1&sort=-1&size=30&ptype=0'.format(albumId)
response_book = requests.get(book_url, headers=headers).json()
book_list = response_book['data']['tracksAudioPlay']
for bl in book_list:
book_title = bl['trackName']
book_id = bl['trackId']
audio_url = 'https://www.ximalaya.com/revision/play/v1/audio?id={}&ptype=1'.format(book_id)
response_audio = requests.get(audio_url, headers=headers).json()
m4a_audio = response_audio['data']['src']
if m4a_audio is not None:
request_m4a_audio = requests.get(m4a_audio, headers=headers).content
# print(book_title)
# print(request_m4a_audio)
with open('有声电子书/{}.mp4'.format(book_title), 'wb') as f:
f.write(request_m4a_audio)
f.close()