文件自动创建在ide本地目录,代码直接复制粘贴就行,我用的是pycharm, 下载包括大屏和小屏图片,如不需要注释掉其中一行就行,还有一行是英雄声音文件,如有需要自己写个,mp3保存
[Python] 纯文本查看 复制代码 from threading import Thread
import requests,os
class Spider(object):
os_path = os.getcwd() + '/英雄联盟图片/'
if not os.path.exists(os_path):
os.mkdir(os_path)
def __init__(self):
self.hero_list_url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
self.headers = {
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0"
}
self.hero_url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'
def parse_hero_list_url(self):
response = requests.get(self.hero_list_url, headers=self.headers).json()
hero_list = response['hero']
for hero in hero_list:
heroId = hero['heroId']
title = hero['name']
name = hero['title']
## selectAudio是英雄声音文件,如需下载自己写个保存音乐的代码,示例: with open(self.os_path + song_name + '.mp3', 'wb') as f: f.write(data)
selectAudio = hero['selectAudio']
os_hero_path = self.os_path + f"/{name}/"
if not os.path.exists(os_hero_path):
os.mkdir(os_hero_path)
hero_url = self.hero_url.format(heroId)
# Thread(target=self.parse_hero_url, args=(hero_url, os_hero_path,name)).start()
self.parse_hero_url(hero_url, os_hero_path, name)
def parse_hero_url(self, hero_url, os_hero_path, name):
response = requests.get(hero_url, headers=self.headers).json()
skins_list = response['skins']
for skin in skins_list:
centerImg = skin["centerImg"]
loadingImg = skin['loadingImg']
skinname = skin['name']
##这一行if是下载电脑端图片的代码,可以注释掉下面手机端
if centerImg:
Thread(target=self.parse_save_skin, args=(os_hero_path, centerImg, name, skinname, "电脑版")).start()
#这一行if是下载手机版的代码,根据自己需要注释相应代码
if loadingImg:
Thread(target=self.parse_save_skin, args=(os_hero_path, loadingImg, name, skinname, "手机版")).start()
def parse_save_skin(self, os_hero_path, img_url, name, skinname, type_img):
skinname = skinname.replace("-", "_").replace("/", "_").replace("\\", "_")
data = requests.get(img_url, headers=self.headers).content
with open(f"{os_hero_path}{skinname} -- {type_img}.jpg", 'wb') as f:
f.write(data)
print(f"英雄 {name}的皮肤 -- {skinname} -- 下载完成")
if __name__=='__main__':
s=Spider()
s.parse_hero_list_url()
|