admin管理员组

文章数量:1619275

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

要求:
1、通过连接

文章目录

    • 一、分析
    • 二、实现代码
    • 三、实现结果

一、分析

1、 https://game.gtimg/images/lol/act/img/js/heroList/hero_list.js 链接里是保存的json数据,通过json解析可以得到:

2、通过英雄id和 https://game.gtimg/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/images/lol/act/img/js/heroList/hero_list.js'
response = requests.get(url, headers=headers).json()
response = response['hero']
url1 = 'https://game.gtimg/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()

三、实现结果

这里只运行一部分:

本文标签: 英雄爬虫皮肤联盟图片