教你用python爬取王者荣耀英雄皮肤图片,并将图片保存在各自英雄的文件夹中。(附源码)

教你用python爬取王者荣耀英雄皮肤图片,并将图片保存在各自英雄的文件夹中。(附源码)
代码展示:
请添加图片描述
保存在各自的文件夹中
在这里插入图片描述

美么?请添加图片描述
让我们开始爬虫之路

开发环境
windows 10
python3.6

引用库存
import requests
import os
import json

打开王者荣耀官网点击游戏资料
https://pvp.qq.com/web201605/herolist.shtml
在这里插入图片描述
在这里插入图片描述
发现herolist.json文件中有英雄的信息,英雄ID、英雄名称,及皮肤的名称
通过英雄ID构造英雄皮肤地址

不需要webdriver 也能快速实现下载图片

import requests
import os
import json

def quid():  
    url='https://pvp.qq.com/web201605/js/herolist.json'
    response=requests.get(url).text
    response=json.loads(response)
    for i in response:
        print(i['ename']) #id
        name=i['cname'] #英雄名字

        id=i['ename']

        word=i.setdefault('skin_name','没有找到')
        print(word)
        b=word.count('|')    #count 查询字符出现的次数 python内置函数
        bb=word.split('|')   #split 分割字符返回列表
        print(b)
        if not os.path.exists(name):   #创建图片保存目录
            os.mkdir(name)
        c = b + 1
        for a in range(0,c):
            aa=a+1
            url4='http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/%s/%s-bigskin-%d.jpg'%(id,id,aa)  #构造英雄皮肤地址
            img2 = requests.get(url4)
            print(url4)
            图片名字 = bb[a]
            with open(name + '/' + 图片名字+'.jpg', 'wb') as f:
                f.write(img2.content)
                print('爬取成功')

if __name__== '__main__':

    quid()

代码仅供学习,欢迎一键三连,感谢各位的支持!
祝大家学习python顺利!

  • 16
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
Python,你可以使用爬虫技术来爬取王者荣耀英雄图片。以下是一个简单的示例代码来演示如何使用Python爬取王者荣耀英雄图片: ```python import requests import os def download_image(url, save_path): response = requests.get(url) with open(save_path, 'wb') as f: f.write(response.content) def crawl_hero_images(): # 创建保存图片文件夹 if not os.path.exists('hero_images'): os.makedirs('hero_images') # 发送请求获取英雄列表 hero_list_url = 'https://api.example.com/heroes' response = requests.get(hero_list_url) hero_list = response.json() # 遍历英雄列表,爬取每个英雄皮肤图片 for hero in hero_list: hero_name = hero['name'] skin_list = hero['skins'] for skin in skin_list: skin_name = skin['name'] image_url = skin['image_url'] save_path = f'hero_images/{hero_name}_{skin_name}.jpg' download_image(image_url, save_path) print(f'Successfully downloaded {hero_name} - {skin_name} image.') crawl_hero_images() ``` 上述代码,我们首先创建了一个`download_image`函数,用于下载图片。然后,我们定义了一个`crawl_hero_images`函数,用于爬取英雄皮肤图片。在该函数,我们首先发送请求获取英雄列表,然后遍历英雄列表,对于每个英雄,再遍历其皮肤列表,获取皮肤图片的URL,并使用`download_image`函数下载图片到本地。 请注意,上述代码的URL和文件路径仅作为示例,请根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值