简易爬虫--50行代码获取英雄联盟全英雄皮肤

50行代码搞定英雄联盟LOL全英雄皮肤下载

# 下载英雄联盟全英雄皮肤
import os
import requests


def downloadPic(heroIds, titles, names,path):
    i = 0
    for j in heroIds:
        # 创建文件夹
        os.mkdir(path + names[i] + '-' + titles[i])
        os.chdir(path + names[i] + '-' + titles[i])
        i += 1
        link = 'https://game.gtimg.cn/images/lol/act/img/js/hero/' + j + '.js'

        res = requests.get(link)  # 请求url
        res = res.json()  # 转化为json格式

        skins = res['skins']
        for k in range(len(skins)):
            imgurl = skins[k]['mainImg']
            if imgurl != '':
                # imgurl = skins[k]['chromaImg']
                # img_title = skins[k]['name'].split(' ')[0]
                im = requests.get(imgurl)  # 请求图片url
                if im.status_code == 200:
                    open(str(k) + '.jpg', 'wb').write(im.content)  # 写入文件
                else:
                    print(link)
                    print(imgurl)


def getData(path):
    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
    herolist = requests.get(url)  # 获取英雄列表json文件
    herolist = herolist.json()  # 转化为json格式
    herolist_json = herolist['hero']

    heroIds = list(map(lambda x: x['heroId'], herolist_json))  # 提取英雄的编号
    titles = list(map(lambda x: x['title'], herolist_json))  # 提取英雄的名称
    names = list(map(lambda x: x['name'], herolist_json))  # 提取英雄的别名

    downloadPic(heroIds, titles, names,path)


if __name__ == '__main__':
    path = "C:\\Users\\admin\\Desktop\\lol\\" # 创建文件夹
    os.mkdir(path)
    getData(path)

思路:该页面抓取的内容实际上是通过请求js拿到的内容进行页面填充,故F12去分析页面请求js,拿到链接,去请求,处理js返回结果就可以了。

注:菜鸟新学的python,请大神们指教

你可能感兴趣的:(python)