Python 爬取LOL英雄壁纸

直接上代码

###导入模块
import requests
from lxml import etree
import requests,json
import sys
import difflib
import os
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}
# https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js 英雄列表
url='https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'

# 文件夹不存在则创建
save_dir = 'LOLHeroSkin'
if not os.path.exists(save_dir):
    os.mkdir(save_dir)

heros=json.loads(requests.get(url,headers=header).content)['hero']
for hero in heros:
    id=int(hero['heroId'])
    url='https://game.gtimg.cn/images/lol/act/img/js/hero/'+str(id)+'.js'
    skins=json.loads(requests.get(url,headers=header).content)['skins']
    for skin in skins:
        name=skin['name'].replace('/','_').replace('“','_').replace('”','_').replace(':','_').replace('"','_')
        imgUrl=skin['mainImg']
        if imgUrl=='': # 炫彩图片
            continue
        img=requests.get(imgUrl,header)
        with open(save_dir+'/'+name+'.jpg',"wb") as file:
            file.write(img.content)
        print(name+'.jpg  下载成功')


print("OK")
posted @ 2020-07-30 15:38  Alex_Mercer  阅读(187)  评论(0编辑  收藏  举报