-- coding: UTF-8 --

"""

@Time: 2021/9/1 23:52

@Author: 远方的星

@CSDN: https://blog.csdn.net/qq_4492...

"""
import os
import json
import requests
import chardet
from tqdm import tqdm
from fake_useragent import UserAgent

随机产生申请头

ua = UserAgent(verify_ssl=False, path='D:/Pycharm/fake_useragent.json')

提前创立一个文件夹,不便创立子文件夹

path_f = "./王者皮肤语音/"
if not os.path.exists(path_f):

os.mkdir(path_f)

随机切换申请头

def random_ua():

headers = {    "accept-encoding": "gzip",  # gzip压缩编码  能进步传输文件速率    "user-agent": ua.random}return headers

下载语音内容

def download(file_name, text, path): # 下载函数

file_path = path + file_namewith open(file_path, 'wb') as f:    f.write(text)    f.close()

获取网页内容并json化

def get_json(page):

url = 'https://m.ximalaya.com/m-revision/common/album/queryAlbumTrackRecordsByPage?'param = {    'albumId': '41725731',    'page': '{}'.format(page),    'pageSize': '10',    'asc': 'true',    'countKeys': 'play', 'comment'    'v': '1630511230862'}res = requests.get(url=url, headers=random_ua(), params=param)res.encoding = chardet.detect(res.content)["encoding"]  # 确定编码格局res = res.texttext_json = json.loads(res)  # 数据json化return text_json

def main():

print("开始下载语音内容^-^")for page in tqdm(range(1, 35)):  # 共337个语音内容,10个一组,所以共须要34组    text_json =[PayPal下载](https://www.gendan5.com/wallet/PayPal.html) get_json(page)    data_s = text_json["data"]["trackDetailInfos"]  # 失去一个寄存信息的列表    for i in range(len(data_s)):        voice_url = data_s[i]["trackInfo"]["playPath"]  # 语音下载地址        voice_name = data_s[i]["trackInfo"]["title"] + '.mp3'  # 语音名称        voice = requests.get(url=voice_url, headers=random_ua()).content  # 获取语音内容        download(voice_name, voice, path_f)  # 下载语音print('所有语音下载结束^-^')

if name == '__main__':

main()