最近在玩英雄联盟,在官网上面看到不同英雄的皮肤,感觉设计的很不错,就想保存下来当做桌面背景。
一张张下载的话速度太慢了,所以就用python写了个代码爬取了一下。
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
import requests
import asyncio
import os
from aiohttp import ClientSession
import aiohttp
import json
from datetime import datetime
async def skins_downloader(semaphore, hero_id, hero_name):
async with semaphore:
url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'.format(hero_id)
dir_name = 'skins/{}'.format(hero_name)
if not os.path.exists(dir_name):
os.mkdir(dir_name)
async with ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
async with session.get(url) as response:
response = await response.read()
for skin in json.loads(response)['skins']:
if skin['mainImg']:
img_url = skin['mainImg']
# kda女团皮肤名带斜杠,replace掉
path = os.path.join(dir_name, '{}.jpg'.format(skin['name'].replace('/', ''), ))
async with session.get(img_url) as skin_response:
with open(path, 'wb') as f:
print('\rDownloading [{:^10}] {:<20}'.format(hero_name, skin['name']), end='')
f.write(await skin_response.read())
def hero_list():
return requests.get('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js').json()['hero']
async def run():
semaphore = asyncio.Semaphore(30)
heroes = hero_list()
tasks = []
for hero in heroes:
tasks.append(asyncio.ensure_future(skins_downloader(semaphore, hero['heroId'], hero['title'])))
await asyncio.wait(tasks)
if __name__ == '__main__':
start_time = datetime.now()
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
loop.close()
end_time = datetime.now()
time_diff = (end_time - start_time).seconds
print('\nTime cost: {}s'.format(time_diff))
|