import requestsimport os# 创建lol文件夹os.mkdir("lol")# 2.读取js文件,获取英雄id(hero_id)url = '/images/lol/act/img/js/heroList/hero_list.js'headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}response = requests.get(url, headers=headers)json_list= response.json()hero_list=json_list['hero']print("英雄总数:",len(hero_list)) try:# 3.根据hero_id拼接URL获取该英雄皮肤地址for m in range(len(hero_list)):# 英雄编号hero_id = hero_list[m]['heroId']print(hero_id)# 英雄名称hero_name = hero_list[m]['name']print(hero_name)url2= '/images/lol/act/img/js/hero/'+hero_id+'.js'headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}response2 = requests.get(url2, headers=headers)json_list2 = response2.json()img_list = json_list2['skins']print(hero_name+"皮肤数量:",len(img_list))# 遍历皮肤地址下载图片for n in range(len(img_list)):imgPath = img_list[n]['mainImg']if(imgPath ==""):continueskin_name = img_list[n]['name'].replace("/"," ")picture = requests.get(imgPath).content# 打印图片网址print(imgPath)# 下载图片 文件路径为: lol/英雄名-皮肤名.jpgwith open('lol/'+ hero_name + ' - ' + skin_name + '.jpg', 'wb') as f:f.write(picture)except KeyError as e:print('程序执行完毕!')
如果觉得《Python爬虫脚本 初级入门爬取英雄联盟所有皮肤(源码)》对你有帮助,请点赞、收藏,并留下你的观点哦!