python 喜马拉雅 音乐下载 演示代码
Posted 电子灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 喜马拉雅 音乐下载 演示代码相关的知识,希望对你有一定的参考价值。
1、主程序文件
import os import json import requests from contextlib import closing from progressbar import ProgressBar from down_line import ProgressBar # start_url = ‘https://www.ximalaya.com/revision/play/album?albumId=‘ # ‘3595841&pageNum={}&sort=-1&pageSize=30‘ # 3595841 分类ID # pageNum={} 分页码 # 运行主目录程序 def xi_ma(): # 找URL start_url = ‘https://www.ximalaya.com/revision/play/album?albumId=‘ ‘9723091&pageNum={}&sort=-1&pageSize=30‘ # 解析url 得到的网页 # 增加header头 简单的反扒技术 headers = { ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ‘ ‘(KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36‘ } for i in range(2): url = start_url.format(i + 1) # 翻页效果相当于提交分页,也就是下一页 print(url) # 提交网址 r = requests.get(url, headers=headers) # 获取数据 ret = r.content.decode(‘utf-8‘) # 转换JSON格式 result = json.loads(ret) # 遍历测试结果 for i in result[‘data‘][‘tracksAudioPlay‘]: # print(i[‘trackName‘], ‘‘ + i[‘src‘]) src = i[‘src‘] name = i[‘trackName‘] # 保存数据 # with open(‘./img/{}.m4a‘ .format(name), ‘ab‘) as f: # f.write(music.content) with closing(requests.get(src, headers=headers, stream=True)) as response: chunk_size = 1024 content_size = int(response.headers[‘content-length‘]) progress = ProgressBar(name, total=content_size, unit=‘KB‘, chunk_size=chunk_size, run_status=‘正在下载‘, fin_status=‘下载完毕‘) if not os.path.exists(‘img‘): os.mkdir(‘img‘) with open(‘./img/{}.m4a‘ .format(name), ‘ab‘) as file: for data in response.iter_content(chunk_size=chunk_size): file.write(data) progress.refresh(count=len(data)) if __name__ == ‘__main__‘: xi_ma()
2.down_lin source 源代码 下载进度条
1 class ProgressBar(object): 2 3 def __init__(self, title, 4 count=0.0, 5 run_status=None, 6 fin_status=None, 7 total=100.0, 8 unit=‘‘, sep=‘/‘, 9 chunk_size=1.0): 10 super(ProgressBar, self).__init__() 11 self.info = "【%s】%s %.2f %s %s %.2f %s" 12 self.title = title 13 self.total = total 14 self.count = count 15 self.chunk_size = chunk_size 16 self.status = run_status or "" 17 self.fin_status = fin_status or " " * len(self.status) 18 self.unit = unit 19 self.seq = sep 20 21 def __get_info(self): 22 # 【名称】状态 进度 单位 分割线 总数 单位 23 _info = self.info % (self.title, self.status, 24 self.count/self.chunk_size, self.unit, self.seq, self.total/self.chunk_size, self.unit) 25 return _info 26 27 def refresh(self, count=1, status=None): 28 self.count += count 29 # if status is not None: 30 self.status = status or self.status 31 end_str = " " 32 if self.count >= self.total: 33 end_str = ‘ ‘ 34 self.status = status or self.fin_status 35 print(self.__get_info(), end=end_str) 36
以上是关于python 喜马拉雅 音乐下载 演示代码的主要内容,如果未能解决你的问题,请参考以下文章
Python 自动化 - 浏览器chrome打开F12开发者工具自动Paused in debugger调试导致无法查看网站资源问题原因及解决方法,javascript反调试问题处理实例演示(代码片段
html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。
sql 这些代码片段将演示如何逐步使用PolyBase。你应该有一个blob存储和存储秘密方便