1. 程式人生 > >python 喜馬拉雅 音樂下載 演示代碼

python 喜馬拉雅 音樂下載 演示代碼

afa space pytho htm 進度 music ges data 增加


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 = "\r" 32 if self.count >= self.total: 33 end_str = ‘\n‘ 34 self.status = status or self.fin_status 35 print(self.__get_info(), end=end_str) 36

python 喜馬拉雅 音樂下載 演示代碼