VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

基本开发环境

  • Python 3.6
  • Pycharm

相关模块的使用

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

 

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

一、明确需求

爬取英雄联盟所有英雄的皮肤背景图。包含炫彩,按照英雄分别保存。

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 

二、网页数据分析

如何找到数据真实地址?

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 


如图所示,皮肤图片url地址 https://game.gtimg.cn/images/lol/act/img/skin/big1001.jpg

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

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

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 


如图所示,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值,那么是需要去列表页面查找。

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 


如图所示,每个英雄的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)
复制代码

 

四、实现效果

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 

Python爬虫新手入门教学(十二):英雄联盟最新皮肤爬取

 


突然发现安妮居然一个炫彩都没有(除了还没出的福牛守护者),但是皮肤是真的多呀
文章出处:https://www.cnblogs.com/hhh188764/p/14367611.html


相关教程