import requests
import json
'''
lol 英雄及英雄皮肤下载
动态加载数据
1.先获取英雄 id
2.拼接 关于图片 js 的 url
3. 请求后将返回体进行数据转换
4.此时的 js 数据(2.js) skines(键名) mainImg(图片url) name(皮肤名字)
'''
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
# 获取英雄 id
def get_id():
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
try:
resp = requests.get(url,headers=headers)
print("获取id数据页面状态响应码:",resp.status_code)
data = json.loads(resp.text)
id_list = []
for i in data['hero']:
id_list.append(i['heroId'])
return id_list
except:
print("获取 id 失败")
return None
# 根据英雄 id
# 获取皮肤图片的名称 及 url
def get_mess(id):
url2 = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'.format(id)
try:
resp2 = requests.get(url2,headers=headers)
data2 = json.loads(resp2.text)
skiname = []
skiurl = []
for g in data2['skins'][0:-1]: # 取出当下所有
skiname.append(g['name'])
skiurl.append(g['mainImg'])
return skiname, skiurl
except:
print(skiname,"获取失败")
return None
# 保存本地
def save_file(skiname,skiurl):
for skinames,skiurls in zip(skiname,skiurl):
try:
resp3 = requests.get(skiurls,headers=headers)
except:
pass
file_path = r'C:\Users\DELL\Desktop\python_wd\mig\lol\{}.png'.format(skinames).replace("/","")
print("<<<<<<<<<<正在下载{}.png >>>>>>".format(skinames))
with open(file_path,'wb')as f:
f.write(resp3.content)
print("当前数据下载完毕。。。。。")
if __name__ == '__main__':
id_list = get_id()
for id in id_list:
skiname, skiurl = get_mess(id)
print("\n当前下载图片数量:",len(skiurl))
save_file(skiname,skiurl)
lol英雄皮肤图片爬取
最新推荐文章于 2022-05-10 11:48:22 发布