爬取英雄联盟所有英雄及皮肤的完整代码:
import requests import os headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"} def get_hero(): url = "https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js" res = requests.get(url).json() for hero in res['hero']: hero_id = hero['heroId'] #获取英雄编号 detail_line = 'https://game.gtimg.cn/images/lol/act/img/js/hero/'+hero_id+'.js' #字符串拼接 #detail_line = 'https://game.gtimg.cn/images/lol/act/img/js/hero/%s.js'%hero_id #python2.5 #detail_line = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js' #字符串格式化python3.6 #detail_line = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'.format(hero_id) #format()形式 get_skin(detail_line) def get_skin(url): res = requests.get(url,headers=headers).json() for skin in res["skins"]: if not skin["mainImg"]: continue item = {} item["heroName"] = skin["heroName"] #英雄的名字 item["skinName"] = skin["name"].replace("/","_") #皮肤的名字并将名字中出现的斜线/用下划线代替_ item["skinImage"] = skin["mainImg"] #皮肤的图片链接 print(item) save(item) def save(item): #构造一个目录 hero_path = '.images/'+item['heroName']+'/' if not os.path.exists(hero_path): #若目录不存在则创建目录 os.makedirs(hero_path) res = requests.get(item["skinImage"]) #发送图片请求 with open(hero_path + item["skinName"]+".png","wb") as f: f.write(res.content) if __name__ == "__main__": get_hero()发现一个不错的爬虫教程,在此分享一下,扫描上方二维码 或 直接在微信上搜索公众号“百里锁钥”,于后台回复“爬虫实战教程”即可获取