Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3593320
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-09-07 17:17:57

# -*- coding: UTF-8 -*-

"""

# @Time: 2021/9/1 23:52

# @Author: 远方的星

# @CSDN: https://blog.csdn.net/qq_44921056

"""

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_name

    with open(file_path, 'wb') as f:

        f.write(text)

        f.close()

# 获取网页内容并json

def get_json(page):

    url = ''

    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.text

    text_json = json.loads(res)  # 数据json

    return text_json

def main():

    print("开始下载语音内容^-^")

    for page in tqdm(range(1, 35)):  # 337个语音内容,10个一组,所以共需要34

        text_json = 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()

阅读(1075) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~