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'

运行效果

相关推荐
m0_7349497916 分钟前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
t***54434 分钟前
Clang 编译器在 Orwell Dev-C++ 中的局限性
开发语言·c++
m0_514520571 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey1 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
oy_mail1 小时前
QoS质量配置
开发语言·智能路由器·php
oyzz1201 小时前
PHP操作redis
开发语言·redis·php
nashane2 小时前
HarmonyOS 6学习:网络能力变化监听与智能提示——告别流量偷跑,打造贴心网络感知应用
开发语言·php·harmony app
Polar__Star2 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
凌波粒2 小时前
Java 8 “新”特性详解:Lambda、函数式接口、Stream、Optional 与方法引用
java·开发语言·idea
m0_743623923 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python