Python爬取王者荣耀全皮肤台词语音

-- 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_name
with 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.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 =[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()

你可能感兴趣的