Python爬虫获取王者荣耀英雄全皮肤图片,并下载到本地

文章目录

下载结果

完整代码

python 复制代码
import requests
import json
import os
import time


def get_hero_info():
    # 英雄的全部信息的url
    hero_info = 'https://pvp.qq.com/web201605/js/herolist.json'
    # 获取英雄的全部信息
    response = requests.get(hero_info)
    # 转为字典格式
    hero_info_dict = json.loads(response.text)
    return hero_info_dict


def downloads_img():
    hero_info_dict = get_hero_info()
    for hero in hero_info_dict:
        # 获取单个英雄的名字
        hero_name = hero['cname']
        # 获取英雄的ID
        hero_num = hero['ename']
        # 图片保存的根路径
        hero_image_path = 'G:\\imgs\\' + hero_name
        # 创建文件夹
        os.mkdir(hero_image_path)
        print(hero_name + '皮肤正在下载....:')
        # 判断英雄是否有皮肤
        if 'skin_name' in hero:
            hero_skins = hero['skin_name']
            # 判断英雄皮肤个数是否大于1
            if '|' in hero_skins:
                # 将英雄的皮肤姓名以 | 分隔开
                hero_skin_list = hero_skins.split('|')
                # 英雄的皮肤个数
                hero_skin_count = len(hero_skin_list)
                for hero_skin_num in range(hero_skin_count):
                    # 英雄的皮肤名字
                    hero_skin_name = hero_skin_list[hero_skin_num]
                    # 英雄皮肤图片的url地址
                    hero_skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str(
                        hero_num) + '/' + str(hero_num) + '-bigskin-' + str(hero_skin_num + 1) + '.jpg'
                    # 将图片转为字节形式
                    image_content = requests.get(hero_skin_url).content  # 请求url
                    # 保存图片
                    with open(hero_image_path + '\\' + hero_name + '-' + hero_skin_name + '.jpg', 'wb') as image:
                        image.write(image_content)
                    print("  【%s】皮肤下载完毕" % hero_skin_name)
        time.sleep(1)


if __name__ == '__main__':
    start = time.time()
    downloads_img()
    end = time.time()
    print('共耗时' + str(end - start) + '秒')

代码解释

  • https://pvp.qq.com/web201605/js/herolist.json是官方的英雄信息数据,格式如下
  • 从中先获取英雄都是有哪些皮肤,并且获取皮肤的名称
  • 根据英雄的ename获取英雄的唯一标识符,进一步获取皮肤的图片地址
python 复制代码
hero_skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str(
                       hero_num) + '/' + str(hero_num) + '-bigskin-' + str(hero_skin_num + 1) + '.jpg'

运行效果

相关推荐
Ai财富密码16 分钟前
Python 爬虫:Selenium 自动化控制(Headless 模式 / 无痕浏览)
爬虫·python·selenium
wjs202435 分钟前
C++ 日期 & 时间
开发语言
终焉代码36 分钟前
【C++】STL二叉搜索树——map与set容器的基础结构
开发语言·数据结构·c++
源代码•宸1 小时前
深入浅出设计模式——行为型模式之观察者模式 Observer
开发语言·c++·经验分享·观察者模式·设计模式·raii
小五1271 小时前
数据科学与计算实例应用
开发语言·python
华科云商xiao徐1 小时前
异步并发×编译性能:Dart爬虫的实战突围
爬虫·数据可视化·dart
站大爷IP1 小时前
Python类型注解:让代码“开口说话”的隐形助手
python
华科云商xiao徐1 小时前
使用reqwest+select实现简单网页爬虫
爬虫
小马敲马2 小时前
[4.2-2] NCCL新版本的register如何实现的?
开发语言·c++·人工智能·算法·性能优化·nccl