import requests, os
# 获取全部英雄信息
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
headers = {
'cookie': 'isActDate=19443; PTTactFirstTime=1679875200000; eas_sid=l14697T9u9P0z805x6r1U396I4; isHostDate=19443; PTTuserFirstTime=1679875200000; pgv_info=ssid=s4080084347; ts_last=lol.qq.com/data/info-heros.shtml; pgv_pvid=7421102125; ts_uid=1884593666; weekloop=0-0-0-13; gpmtips_cfg={"iSendApi":0,"iShowCount":0,"iOnlineCount":0,"iSendOneCount":0,"iShowAllCount":0,"iHomeCount":0}; lolqqcomrouteLine=data_data',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
response = requests.get(url, headers=headers)
response.encoding = 'gb2312'
result = response.json()
# print(result['hero'][0])
# 循环获取每个英雄信息
for x in result['hero']:
# 获取英雄的ID
heroId = x['heroId']
# 获取每个英雄的地址
url1 = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{heroId}.js'
response1 = requests.get(url1, headers=headers)
response1.encoding = 'gb2312'
result1 = response1.json()
# 给每个英雄创建一个文件夹
os.mkdir(rf'./files/{x["name"]}')
# 遍历英雄的每个皮肤
for y in result1['skins']:
# 获取皮肤的网络地址
url_img = y['mainImg']
# 判断地址是否为空
if url_img:
response_img = requests.get(url_img)
result_img = response_img.content
# 判断皮肤名字里面是否有'/'
if '/' in y["name"]:
y["name"] = y["name"].replace('/', '-')
# 保存到对应文件夹
with open(rf'./files/{x["name"]}/{y["name"]}.{url_img[-3:]}', 'wb') as f:
f.write(result_img)
day16-爬虫作业
最新推荐文章于 2024-09-27 10:11:28 发布