网址:https://pvp.qq.com/story201904/index.html#/hero
import requests
import json
import os
import re
import time
def create_folder(path):
if not os.path.exists(path):
os.makedirs(path)
print(f"文件夹 '{path}' 创建成功")
else:
print(f"文件夹 '{path}' 已存在")
def download_mp3(url, save_path):
if os.path.exists(save_path):
print('文件已经下载过:' + save_path)
else:
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print("下载完成!"+save_path+url)
else:
print("下载失败!"+save_path+url)
time.sleep(1)
url = 'https://pvp.qq.com/webplat/info/news_version3/15592/18024/23901/24397/24398/m17330/list_1.shtml?callback=createHeroList'
# url = 'https://pvp.qq.com/story201904/index.html#/hero'
path = '王者荣耀语音包'
create_folder(path)
response = requests.get(url=url)
response.encoding = 'utf-8'
content = response.text
# print(content)
content = content.split('(')[1].split(')')[0]
json_data = json.loads(content)
print(json_data)
for data in json_data['data']:
if not data:
continue
heroid = data['heroid']
title_path = path + '/' + data['title']
create_folder(title_path)
heroid_url = "https://pvp.qq.com/zlkdatasys/storyhero/index" + heroid + ".json"
response = requests.get(url=heroid_url)
response.encoding = 'utf-8'
heroid_content = response.text
# print(content)
heroid_data = json.loads(heroid_content)
print(heroid_data)
# 不存在语音时跳过
if 'yy_4e' not in heroid_data:
continue
for item in heroid_data['yy_4e']:
name = item['yywa1_f2']
# 删除文件名中的特殊字符:检查文件名,删除斜杠 / 和问号 ? 这样的特殊字符。确保文件名只包含有效的字符和合法的文件名扩展名
name = re.sub(r'[\\/:*?"<>|]', '', name)
mp3name = title_path + '/' + name + '.mp3'
mp3url = 'https:' + item['yyyp_9a']
# print(mp3url, mp3name)
download_mp3(mp3url, mp3name)