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'

运行效果

相关推荐
序属秋秋秋1 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
木头左2 小时前
逻辑回归的Python实现与优化
python·算法·逻辑回归
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
quant_19863 小时前
R语言如何接入实时行情接口
开发语言·经验分享·笔记·python·websocket·金融·r语言
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
风吹落叶花飘荡7 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript
失败又激情的man8 小时前
python之requests库解析
开发语言·爬虫·python
爬虫程序猿8 小时前
利用爬虫按关键字搜索淘宝商品实战指南
android·爬虫
打酱油的;8 小时前
爬虫-数据解析
爬虫
打酱油的;8 小时前
爬虫-request处理get
爬虫·python·django