python 爬取英雄聯(lián)盟皮膚并下載的示例
爬取結(jié)果:
爬取代碼
import osimport jsonimport requestsfrom tqdm import tqdmdef lol_spider(): # 存放英雄信息 heros = [] # 存放英雄皮膚 hero_skins = [] # 獲取所有英雄信息 url = ’https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js’ hero_text = requests.get(url).text # 轉(zhuǎn)為 json 格式 hero_json = json.loads(hero_text)[’hero’] path = os.getcwd() # 獲取當(dāng)前文件夾路徑 workspace = os.getcwd() # 皮膚路徑 skin_path = '{}{}'.format(workspace, ’skins’) # 遍歷列表 for hero in hero_json: # 將每一個(gè)英雄的 id、name 放入一個(gè)字典中 hero_dict = {’id’: hero[’heroId’], ’name’: hero[’name’]} # 放入列表 heros.append(hero_dict) # 遍歷列表 for hero in heros: hero_id = hero[’id’] hero_name = hero[’name’] # 為每一個(gè)英雄創(chuàng)建一個(gè)以自己名字命名的文件夾,用來(lái)存放皮膚圖片 dir_name = skin_path + ’{}’.format(hero_name) if not os.path.exists(dir_name): os.mkdir(dir_name) # 進(jìn)入文件夾 os.chdir(dir_name) # 根據(jù)每一個(gè)英雄的 id 生成皮膚信息的 url hero_skin_url = ’https://game.gtimg.cn/images/lol/act/img/js/hero/’ + hero_id + ’.js’ # 通過(guò) url 獲取英雄的皮膚數(shù)量 skin_text = requests.get(hero_skin_url).text skin_json = json.loads(skin_text) skin_list = skin_json[’skins’] # 獲取皮膚名 hero_skins.clear() for skin in skin_list: hero_skins.append(skin[’name’].replace(’/’, ’’).replace(’’, ’’).replace(’ ’, ’’)) # 皮膚數(shù)量 skins_num = len(hero_skins) s = ’’ for i in tqdm(range(skins_num), desc=’【’ + hero_name + ’】皮膚下載’): if len(str(i)) == 1:s = ’00’ + str(i) elif len(str(i)) == 2:s = ’0’ + str(i) elif len(str(i)) == 3:pass try:# 拼接指定皮膚的 urlskin_url = ’https://game.gtimg.cn/images/lol/act/img/skin/big’ + hero_id + ’’ + s + ’.jpg’img = requests.get(skin_url) except:# 沒(méi)有炫彩皮膚 url 則跳過(guò)continue # 保存皮膚圖片 if img.status_code == 200:with open(hero_skins[i] + ’.jpg’, ’wb’) as f: f.write(img.content)if __name__ == ’__main__’: lol_spider()
以上就是python 爬取英雄聯(lián)盟皮膚并下載的示例的詳細(xì)內(nèi)容,更多關(guān)于python 爬取英雄聯(lián)盟皮膚的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python 寫(xiě)一個(gè)文件分發(fā)小程序2. python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別3. 如何基于Python和Flask編寫(xiě)Prometheus監(jiān)控4. Python Selenium破解滑塊驗(yàn)證碼最新版(GEETEST95%以上通過(guò)率)5. Python中Anaconda3 安裝gdal庫(kù)的方法6. 用python對(duì)oracle進(jìn)行簡(jiǎn)單性能測(cè)試7. Python自動(dòng)化之定位方法大殺器xpath8. Vue3中使用this的詳細(xì)教程9. Python QT組件庫(kù)qtwidgets的使用10. Python 利用flask搭建一個(gè)共享服務(wù)器的步驟
