from urllib import request,parse from bs4 import BeautifulSoup import os import time,random import re,json def work(bh,url1,url2): if os.path.isdir('d:/ximalaya/%s'%bh) == False: os.makedirs('d:/ximalaya/%s'%bh) conten = [] headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/5.0 Chrome/61.0.3163.79 Safari/537.36', 'Upgrade-Insecure-Requests': '1', 'Host':'www.ximalaya.com', 'Referer':'https://www.ximalaya.com/renwen/%s/'%bh, } req = request.Request(url1,headers = headers) page = request.urlopen(req,timeout=6) html = page.read().decode('utf-8') html_json = json.loads(html) page = html_json['data']['trackTotalCount'] page_cont = page//30 +2 for ii in range(1,page_cont): url ='https://www.ximalaya.com/revision/play/album?albumId=%s&pageNum=%s&sort=-1&pageSize=30'%(bh,str(ii)) req = request.Request(url, headers=headers) page = request.urlopen(req, timeout=6) html = page.read().decode('utf-8') html_json = json.loads(html) titles = html_json['data']['tracksAudioPlay'] for ii in titles: if os.path.exists('D:\ximalaya\'+bh+'\'+ii['trackName']) == False: print('下载%s中........'%ii['trackName']) request.urlretrieve(ii['src'],'D:\ximalaya\'+bh+'\'+ii['trackName']) sjz = random.randint(3, 9) print('随机等待%d秒' % sjz) time.sleep(sjz) else: print('文件已经存在--%s'%ii['trackName']) if __name__ == '__main__': #bh = input('请输入音频网址的编码号:') bh_list = ['4505461','5857398'] for bh in bh_list: url1 = 'https://www.ximalaya.com/revision/album/getTracksList?albumId=%s&pageNum=1' % bh url2 = 'https://www.ximalaya.com/revision/play/album?albumId=%s'%bh work(bh,url1,url2)
根据网络的授课视频,自己进行了完善,直接定义好音频专辑的编辑,直接下载到D盘的ximalaya的文件夹下,再用格式工厂转换成MP3格式即可了。