爬取王者荣耀语音包

网址: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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值