爬虫获取王者荣耀全英雄皮肤

爬虫获取王者荣耀全英雄皮肤

# coding=utf-8

import os,requests,shutil


"""
王者荣耀全英雄皮肤
"""


def getHeroListInfo(url=u'https://pvp.qq.com/web201605/js/herolist.json'):
    """
    获取到英雄列表信息
    :param url:
    :return:hero_name_list,hero_number_list
    """
    herolist = requests.get(url)  # 获取英雄列表json文件
    herolist_json = herolist.json()  # 转化为json格式
    hero_name_list = list(map(lambda x: x['cname'], herolist.json()))  # 提取英雄的名字
    hero_number_list = list(map(lambda x: x['ename'], herolist.json()))  # 提取英雄的编号
    return hero_name_list, hero_number_list

# 下载图片
def downloadPic(storageDir="data",
                downloadUrl=u'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/',
                max_num = 20):
    """
    下载图片
    :param storageDir: 储存目录
    :param downloadUrl:下载路径
    :param max_num: 最大皮肤种类个数
    :return:
    """

    #记录当前目录
    current_dir = os.getcwd()
    if not os.path.exists(storageDir):
        os.mkdir(storageDir)
    else:
        shutil.rmtree(storageDir)
        os.mkdir(storageDir)

    print("[INFO]: 储存目录已清空", os.path.join(current_dir,storageDir))

    hero_name_list, hero_number_list = getHeroListInfo()

    print("[INFO]: 已获取到英雄列表信息:\n",
          hero_name_list,
          hero_number_list)

    i = 0
    for hero_number in hero_number_list:
        hero_name = hero_name_list[i]
        print("[INFO]: 获取",hero_name,"英雄图片中...")
        #恢复目录
        os.chdir(current_dir)
        hero_dir = os.path.join(storageDir, hero_name)
        # 创建文件夹
        if not os.path.exists(hero_dir):
            os.mkdir(hero_dir)
        # 进入创建好的文件夹
        os.chdir(hero_dir)
        i += 1
        hero_count = 0
        for k in range(max_num):
            # 拼接url
            onehero_link = downloadUrl + str(hero_number) + '/' + str(hero_number) + '-bigskin-' + str(k) + '.jpg'
            im = requests.get(onehero_link)  # 请求url
            if im.status_code == 200:
                picture_name = "_".join([hero_name, str(hero_number) , str(k)]) + '.jpg'
                open(picture_name, 'wb').write(im.content)  # 写入文件
                print("[INFO]:下载完成:", os.path.join(hero_dir,picture_name))
                hero_count += 1
        print("[INFO]: 已获取", hero_name,"个数:",hero_count,"英雄图片完成...")
    os.chdir(current_dir)



if __name__ == '__main__':
    downloadPic()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lanlingxueyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值