一、引言
在王者荣耀这个热门游戏中,英雄形象各具特色,深受玩家喜爱。为了方便对这些英雄图片进行整理和研究,本文将详细介绍如何爬取王者荣耀官网的全英雄图片,并按照英雄名字进行命名。
二、准备工作
- 熟悉 Python 编程语言,特别是相关的网络请求和文件操作库。
- 安装所需的库,如
requests
用于发送网络请求,BeautifulSoup
用于解析 HTML 等。
三、分析王者荣耀官网结构
通过浏览器开发者工具,观察王者荣耀官网中英雄图片的加载方式和存储路径。
四、编写爬取代码
注意:需在该py文件同级目录下创建一个名为head的文件用来存储图片
from urllib import request
import json
response = request.urlopen("https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2867062")
response = response.read().decode("utf8")
response = json.loads(response)['hero']
print(response)
for dates in response:
head_url = f"https://game.gtimg.cn/images/lol/act/img/skinloading/{dates['instance_id']}.jpg"
print(head_url)
cname = dates['title']
# print(cname, head_url)
head_response = request.urlopen(head_url)
head_response = head_response.read()
file = open(f"./head/{cname}.jpg", "wb")
file.write(head_response)
file.close()
print(f"保存 {cname} 成功")