Python爬虫:英雄联盟英雄皮肤图片

进入英雄联盟官网的英雄链接https://lol.qq.com/data/info-heros.shtml,发现内容并不是储存在静态网页中,通过查看元素,找寻到了接口https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js ,进行以下操作:

要求:
1、通过连接

一、分析

1、 https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js 链接里是保存的json数据,通过json解析可以得到:
在这里插入图片描述

2、通过英雄id和 https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js进行拼接,这个英雄所有的皮肤的链接都保存在里面。通过json解析可以得到:
在这里插入图片描述

3、保存时我们以英雄的名称名字来命名文件夹,每个皮肤的图片以皮肤名称命名

二、实现代码

import requests
import os

headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
response = requests.get(url, headers=headers).json()
response = response['hero']
url1 = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'

for i in response:
    heroId = i['heroId']
    name = i['name']
    title = i['title']
    if os.path.isdir('./img/{}'.format(name+' '+title)):
        print('ok')
    else:
        os.mkdir('./img/{}'.format(name+' '+title))
    url2 = url1.format(heroId)
    response1 = requests.get(url2, headers=headers).json()
    picture_list = response1['skins']
    for j in picture_list:
        picture_url = j['mainImg']
        skin_name = j['name']
        if picture_url:
            response2 = requests.get(picture_url, headers=headers).content
            file = open('./img/{}/{}.jpg'.format(name+' '+title, skin_name), 'wb')
            file.write(response2)
            file.close()

三、实现结果

这里只运行一部分:
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值