爬虫-下载英雄联盟所有英雄的皮肤
import requests
import os
def download_pifu(ip):
response = requests.get(ip)
result = response.json()
# print(result)
for x in result['hero']:
name = x['name']
hero_id = x['heroId']
directory = 'files/pifu/' + name
if not os.path.exists(directory):
os.makedirs(directory)
ip1 = 'https://game.gtimg.cn/images/lol/act/img/js/hero/' + hero_id + '.js'
# print(ip)
response1 = requests.get(ip1)
result1 = response1.json()
for y in result1['skins']:
name1 = y['name'].replace('/', ' ')
# name = y['heroName']
result2 = y['mainImg']
try:
response2 = requests.get(url=result2)
result3 = response2.content
print(type(result3))
with open('files/pifu/' + name + '/' + name1 + '.jpg', 'wb') as f:
f.write(result3)
print(f'------{name1}下载完成-------')
except:
continue
print(download_pifu('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'))
-
根据英雄联盟所有角色页面的json,获取数据结果,可以得到每个英雄的id,
-
由于每个角色皮肤的json的地址只有结尾不一样(并且是以英雄id.js为结尾),那么每个英雄皮肤的json都需要字符串拼接
-
向每个角色皮肤的json发送请求得到结果,可以得到每个皮肤的名字和皮肤地址,由于部分皮肤名字存在’/',可以替换一下
-
由于此时得到的还是皮肤的字符串地址,要下载该皮肤还需要根据该地址,发送请求,得到数据进行下载
-
由于角色众多,每个角色又有多个皮肤,可以在files/pifu下以角色名为目录,分类存放皮肤(files/pifu目录需要提前创建好)
-
由于不是每个皮肤都会有皮肤地址,有些皮肤没有’mainImg’,所以需要有一个异常捕获,跳过改皮肤,也可以下载’chromImg’
(只截取的部分)