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'

运行效果

相关推荐
§ꦿCFོ༉3 小时前
嵌入式八股文—C语言入门学习(四)
c语言·开发语言·学习
投图匠3 小时前
十种不同风格的 CTP接口 框架
开发语言·python
2601_967980115 小时前
深入理解词向量算法及其在Python中的实践
python·word2vec·glove·fasttext·词向量
129Lab5 小时前
电池循环测试数据自动化分析:用Python+AI工具构建一键式寿命评估流水线
python·数据分析·电池测试·数据自动化·寿命评估
treasure1405 小时前
【复古】浅尝Django前后端不分离
前端·css·python·django·bootstrap·html·web
Dmz丶6 小时前
前端转 AI 100 天 Day 44:前端集成实战——用 React 给 Agent 做个正式界面
python
jay神6 小时前
基于 YOLOv8 的手势识别系统(Web 版)
前端·人工智能·python·深度学习·yolo·计算机视觉·计算机毕业设计
DeepVisionary6 小时前
阿里云上线旗舰多模态新模型,不公布跑分且限时14天退役
python·自动化
东莞市云毅网络有限公司6 小时前
FAQ 覆盖度打分:TF-IDF 与向量嵌入双路召回的 Python 实现
python·数据清洗·rag·企业知识库·文档解析
爱笑的k117 小时前
nano-vllm 源码剖析:千行 Python 手写离线推理引擎的调度、显存与并行时序
开发语言·python·vllm