Python爬虫入门教程12:英雄联盟皮肤图片的爬取

前言💨

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

前文内容💨

Python爬虫入门教程01:豆瓣Top电影爬取

Python爬虫入门教程02:小说爬取

Python爬虫入门教程03:二手房数据爬取

Python爬虫入门教程04:招聘信息爬取

Python爬虫入门教程05:B站视频弹幕的爬取

Python爬虫入门教程06:爬取数据后的词云图制作

Python爬虫入门教程07:腾讯视频弹幕爬取

Python爬虫入门教程08:爬取csdn文章保存成PDF

Python爬虫入门教程09:多线程爬取表情包图片

Python爬虫入门教程10:彼岸壁纸爬取

Python爬虫入门教程11:新版王者荣耀皮肤图片的爬取

PS:如有需要 Python学习资料 以及 解答 的小伙伴可以加点击下方链接自行获取
python免费学习资料以及群交流解答点击即可加入

基本开发环境💨

  • Python 3.6
  • Pycharm

相关模块的使用💨

import os    # 内置模块 用于创建文件
import requests     # 第三方模块 需要 pip install requests 安装  用于请求网页数据

安装Python并添加到环境变量,pip安装需要的相关模块即可。

一、💥明确需求

爬取英雄联盟所有英雄的皮肤背景图。包含炫彩,按照英雄分别保存。
在这里插入图片描述

二、💥网页数据分析

如何找到数据真实地址?

在这里插入图片描述
如图所示,皮肤图片url地址 https://game.gtimg.cn/images/lol/act/img/skin/big1001.jpg

每张图片的url地址都是根据后面的 big1001 改变的而一一对应的。

所以可以复制 big1001 在开发者工具里面进行搜索,查找一下图片地址的来源。

在这里插入图片描述
如图所示,https://game.gtimg.cn/images/lol/act/img/js/hero/1.js 链接中皮肤的名字,图片地址,英雄名字,都有了。

既然找到了图片来源的地方,那么就要找上面这个数据接口的来源了。

安妮数据接口: https://game.gtimg.cn/images/lol/act/img/js/hero/1.js
安妮数据详情页: https://lol.qq.com/data/info-defail.shtml?id=1

奥拉夫数据接口: https://game.gtimg.cn/images/lol/act/img/js/hero/2.js
奥拉夫数据详情页: https://lol.qq.com/data/info-defail.shtml?id=2

通过上面的链接对比,可以清楚的看到,接口数据的参数变化是根据英雄ID来的。

一般情况如果是想要获取每个页面的ID值,那么是需要去列表页面查找。
在这里插入图片描述
在这里插入图片描述
如图所示,每个英雄的ID就都有了。

三、💥代码实现

1、获取所有英雄ID

    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
    json_data = get_response(url).json()['hero']
    for i in json_data:
        hero_id = i['heroId']

2、每张英雄图片

def get_hero_url(hero_id):
    page_url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js'
    hero_data = get_response(page_url).json()
    skins = hero_data['skins']
    for index in skins:
        # 皮肤url
        image_url = index['mainImg']
        # 皮肤名字
        hero_name = index['name']
        # 文件夹名字
        hero_title = index['heroTitle']
        if image_url:
            save(hero_title, hero_name, image_url)
        else:
            image_2_url = index['chromaImg']
            save(hero_title, hero_name, image_2_url)

这里需要进行一个判断,因为有一些英雄皮肤是携带炫彩的。

3、保存数据(数据持久化)

def save(hero_title, hero_name, image_url):
    path = f'{hero_title}\\'
    if not os.path.exists(path):
        os.makedirs(path)
    image_content = get_response(image_url).content
    with open(path + hero_name + '.jpg', mode='wb') as f:
        f.write(image_content)
        print('正在保存:', hero_name)

四、💥实现效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
突然发现安妮居然一个炫彩都没有,但是皮肤是真的多呀

posted @ 2021-02-01 14:55  有趣的Python  阅读(296)  评论(0编辑  收藏  举报