|
| 1 | +from gevent import monkey; monkey.patch_all() |
| 2 | +import requests |
| 3 | +import gevent |
| 4 | +import os |
| 5 | +import re |
| 6 | + |
| 7 | + |
| 8 | +header = { |
| 9 | + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36' |
| 10 | +} |
| 11 | + |
| 12 | +root_path = 'D:\LOL' |
| 13 | + |
| 14 | +def mkdir(path): |
| 15 | + if not os.path.exists(path): |
| 16 | + os.mkdir(path) |
| 17 | + |
| 18 | +def crawling(): |
| 19 | + url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js' |
| 20 | + resp = requests.get(url=url, headers=header) |
| 21 | + heros = resp.json()['hero'] |
| 22 | + index = 0 |
| 23 | + task_list = [] |
| 24 | + for hero in heros: |
| 25 | + index = index + 1 |
| 26 | + |
| 27 | + heroId = hero['heroId'] |
| 28 | + hero_url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{heroId}.js' |
| 29 | + hero_resp = requests.get(url=hero_url, headers=header) |
| 30 | + skins = hero_resp.json()['skins'] |
| 31 | + |
| 32 | + task = gevent.spawn(get_pic, skins) |
| 33 | + task_list.append(task) |
| 34 | + if len(task_list) == 10 or len(skins) == index: |
| 35 | + gevent.joinall(task_list) |
| 36 | + task_list = [] |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +def get_pic(skins): |
| 41 | + for skin in skins: |
| 42 | + |
| 43 | + dir_name = skin['heroName'] + '_' + skin['heroTitle'] |
| 44 | + pic_name = ''.join(skin['name'].split(skin['heroTitle'])).strip(); |
| 45 | + url = skin['mainImg'] |
| 46 | + |
| 47 | + if not url: |
| 48 | + continue |
| 49 | + |
| 50 | + invalid_chars='[\\\/:*?"<>|]' |
| 51 | + pic_name = re.sub(invalid_chars,'', pic_name) |
| 52 | + download(dir_name, pic_name, url) |
| 53 | + |
| 54 | +def download(dir_name, pic_name, url): |
| 55 | + print(f'{pic_name} 下载完成, {url}') |
| 56 | + dir_path = f'{root_path}\{dir_name}' |
| 57 | + mkdir(dir_path) |
| 58 | + |
| 59 | + resp = requests.get(url, headers=header) |
| 60 | + with open(f'{dir_path}\{pic_name}.png', 'wb') as f: |
| 61 | + f.write(resp.content) |
| 62 | + print(f'{pic_name} 下载完成') |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + mkdir(root_path) |
| 66 | + crawling() |
0 commit comments