爬取结果:
爬取代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import os import json import requests from tqdm import tqdm def lol_spider(): # 存放英雄信息 heros = [] # 存放英雄皮肤 hero_skins = [] # 获取所有英雄信息 url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js' hero_text = requests.get(url).text # 转为 json 格式 hero_json = json.loads(hero_text)[ 'hero' ] path = os.getcwd() # 获取当前文件夹路径 workspace = os.getcwd() # 皮肤路径 skin_path = "{}\\{}" . format (workspace, 'skins' ) # 遍历列表 for hero in hero_json: # 将每一个英雄的 id、name 放入一个字典中 hero_dict = { 'id' : hero[ 'heroId' ], 'name' : hero[ 'name' ]} # 放入列表 heros.append(hero_dict) # 遍历列表 for hero in heros: hero_id = hero[ 'id' ] hero_name = hero[ 'name' ] # 为每一个英雄创建一个以自己名字命名的文件夹,用来存放皮肤图片 dir_name = skin_path + '\\{}' . format (hero_name) if not os.path.exists(dir_name): os.mkdir(dir_name) # 进入文件夹 os.chdir(dir_name) # 根据每一个英雄的 id 生成皮肤信息的 url hero_skin_url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/' + hero_id + '.js' # 通过 url 获取英雄的皮肤数量 skin_text = requests.get(hero_skin_url).text skin_json = json.loads(skin_text) skin_list = skin_json[ 'skins' ] # 获取皮肤名 hero_skins.clear() for skin in skin_list: hero_skins.append(skin[ 'name' ].replace( '/' , ' ').replace(' \\ ', ' ').replace(' ', ' ')) # 皮肤数量 skins_num = len (hero_skins) s = '' for i in tqdm( range (skins_num), desc = '【' + hero_name + '】皮肤下载' ): if len ( str (i)) = = 1 : s = '00' + str (i) elif len ( str (i)) = = 2 : s = '0' + str (i) elif len ( str (i)) = = 3 : pass try : # 拼接指定皮肤的 url skin_url = 'https://game.gtimg.cn/images/lol/act/img/skin/big' + hero_id + ' ' + s + ' .jpg' img = requests.get(skin_url) except : # 没有炫彩皮肤 url 则跳过 continue # 保存皮肤图片 if img.status_code = = 200 : with open (hero_skins[i] + '.jpg' , 'wb' ) as f: f.write(img.content) if __name__ = = '__main__' : lol_spider() |
以上就是python 爬取英雄联盟皮肤并下载的示例的详细内容,更多关于python 爬取英雄联盟皮肤的资料请关注服务器之家其它相关文章!
原文链接:https://mp.weixin.qq.com/s?__biz=MzkwNjEzMTI4NA==&mid=2247491777&idx=1&sn=901480eb3b840b4ead00a8817123d2e4&chksm=c0ef82faf7980becf5043a026f3d12de7efb9f2bb0bc0de068c1f82df9363fcd9275ad173510&token=1987629775&lang=zh_CN#rd