python 爬取英雄联盟皮肤并下载的示例
爬取结果:
爬取代码
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 爬取英雄联盟皮肤的资料请关注我们其它相关文章!
赞 (0)