前言??
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
前文內容??
Python爬蟲入門教程01:豆瓣Top電影爬取
Python爬蟲入門教程02:小說爬取
Python爬蟲入門教程03:二手房資料爬取
Python爬蟲入門教程04:招聘資訊爬取
Python爬蟲入門教程05:B站視頻彈幕的爬取
Python爬蟲入門教程06:爬取資料后的詞云圖制作
Python爬蟲入門教程07:騰訊視頻彈幕爬取
Python爬蟲入門教程08:爬取csdn文章保存成PDF
Python爬蟲入門教程09:多執行緒爬取表情包圖片
Python爬蟲入門教程10:彼岸壁紙爬取
Python爬蟲入門教程11:新版王者榮耀皮膚圖片的爬取
PS:如有需要 Python學習資料 以及 解答 的小伙伴可以加點擊下方鏈接自行獲取
python免費學習資料以及群交流解答點擊即可加入
基本開發環境??
- Python 3.6
- Pycharm
相關模塊的使用??
import os # 內置模塊 用于創建檔案
import requests # 第三方模塊 需要 pip install requests 安裝 用于請求網頁資料
安裝Python并添加到環境變數,pip安裝需要的相關模塊即可,
一、??明確需求
爬取英雄聯盟所有英雄的皮膚背景圖,包含炫彩,按照英雄分別保存,

二、??網頁資料分析
如何找到資料真實地址?

如圖所示,皮膚圖片url地址 https://game.gtimg.cn/images/lol/act/img/skin/big1001.jpg
每張圖片的url地址都是根據后面的 big1001 改變的而一一對應的,
所以可以復制 big1001 在開發者工具里面進行搜索,查找一下圖片地址的來源,

如圖所示,https://game.gtimg.cn/images/lol/act/img/js/hero/1.js 鏈接中皮膚的名字,圖片地址,英雄名字,都有了,
既然找到了圖片來源的地方,那么就要找上面這個資料介面的來源了,
安妮資料介面: https://game.gtimg.cn/images/lol/act/img/js/hero/1.js
安妮資料詳情頁: https://lol.qq.com/data/info-defail.shtml?id=1
奧拉夫資料介面: https://game.gtimg.cn/images/lol/act/img/js/hero/2.js
奧拉夫資料詳情頁: https://lol.qq.com/data/info-defail.shtml?id=2
通過上面的鏈接對比,可以清楚的看到,介面資料的引數變化是根據英雄ID來的,
一般情況如果是想要獲取每個頁面的ID值,那么是需要去串列頁面查找,


如圖所示,每個英雄的ID就都有了,
三、??代碼實作
1、獲取所有英雄ID
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
json_data = https://www.cnblogs.com/Qqun821460695/p/get_response(url).json()['hero']
for i in json_data:
hero_id = i['heroId']
2、每張英雄圖片
def get_hero_url(hero_id):
page_url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js'
hero_data = https://www.cnblogs.com/Qqun821460695/p/get_response(page_url).json()
skins = hero_data['skins']
for index in skins:
# 皮膚url
image_url = index['mainImg']
# 皮膚名字
hero_name = index['name']
# 檔案夾名字
hero_title = index['heroTitle']
if image_url:
save(hero_title, hero_name, image_url)
else:
image_2_url = index['chromaImg']
save(hero_title, hero_name, image_2_url)
這里需要進行一個判斷,因為有一些英雄皮膚是攜帶炫彩的,
3、保存資料(資料持久化)
def save(hero_title, hero_name, image_url):
path = f'{hero_title}\\'
if not os.path.exists(path):
os.makedirs(path)
image_content = get_response(image_url).content
with open(path + hero_name + '.jpg', mode='wb') as f:
f.write(image_content)
print('正在保存:', hero_name)
四、??實作效果




突然發現安妮居然一個炫彩都沒有,但是皮膚是真的多呀
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255470.html
標籤:Python
