用Python爬取英雄联盟的皮肤详细示例
作者:编程简单学 时间:2023-05-07 22:41:19
一、推理原理
1.先去《英雄联盟》官网找到英雄及皮肤图片的网址:
lol.qq.com
2.从上面网址可以看到所有英雄都在,按下F12查看源代码,发现英雄及皮肤图片并没有直接给出,而是隐藏在JS文件中。
这时候需要点开Network,找到js窗口,刷新网页,就看到一个champion.js的选项,点击可以看到一个字典——里面就包含了所有英雄的名字(英文)以及对应的编号。
3.但是只有英雄的名字(英文)以及对应的编号并不能找到图片地址,于是回到网页,随便点开一个英雄,跳转页面后发现英雄及皮肤的图片都在,但要下载还需要找到原地址,这是鼠标右击选择“在新标签页中打开”,新的网页才是图片的原地址。
4.图中红色框就是我们需要的图片地址,经过分析知道:每一个英雄及皮肤的地址只有编号不一样
二、推理代码
第一步:获取js字典
def path_js(url_js):
res_js = requests.get(url_js, verify = False).content
html_js = res_js.decode("gbk")
pat_js = r'"keys":(.*?),"data"'
enc = re.compile(pat_js)
list_js = enc.findall(html_js)
dict_js = eval(list_js[0])
return dict_js
第二步:从 js字典中提取到key值生成url列表
def path_url(dict_js):
pic_list = []
for key in dict_js:
for i in range(20):
xuhao = str(i)
if len(xuhao) == 1:
num_houxu = "00" + xuhao
elif len(xuhao) == 2:
num_houxu = "0" + xuhao
numStr = key+num_houxu
url = r'http://ossweb-img.qq.com/images/lol/web201310/skin/big'+numStr+'.jpg'
pic_list.append(url)
print(pic_list)
return pic_list
第三步:从 js字典中提取到value值生成name列表
def name_pic(dict_js, path):
list_filePath = []
for name in dict_js.values():
for i in range(20):
file_path = path + name + str(i) + '.jpg'
list_filePath.append(file_path)
return list_filePath
第四步:下载并保存数据
def writing(url_list, list_filePath):
try:
for i in range(len(url_list)):
res = requests.get(url_list[i], verify = False).content
with open(list_filePath[i], "wb") as f:
f.write(res)
except Exception as e:
print("下载图片出错,%s" %(e))
return False
第五步:执行主程序
if __name__ == '__main__':
url_js = r'http://lol.qq.com/biz/hero/champion.js'
path = r'./data/' #图片存在的文件夹
dict_js = path_js(url_js)
url_list = path_url(dict_js)
list_filePath = name_pic(dict_js, path)
writing(url_list, list_filePath)
运行后会在控制台打印出每一张图片的网址:
在文件夹中可以看到图片已经下载好 如图:
来源:https://blog.csdn.net/weixin_54556126/article/details/121656496
标签:Python,英雄联盟,皮肤图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL字符集 GBK、GB2312、UTF8区别 解决MYSQL中文乱码问题
2024-01-14 03:16:56
Elasticsearch之倒排索引及索引操作
2023-10-28 00:38:38
js判断复选框是否选中及选中个数的实现代码
2024-04-22 22:17:03
一文了解Python 流程控制
2021-01-27 18:37:28
![](https://img.aspxhome.com/file/2023/5/93805_0s.png)
win10下mysql 5.7.23 winx64安装配置方法图文教程
2024-01-18 07:28:06
![](https://img.aspxhome.com/file/2023/9/121269_0s.png)
golang 监听服务的信号,实现平滑启动,linux信号说明详解
2024-05-09 10:00:43
![](https://img.aspxhome.com/file/2023/1/127041_0s.png)
命令行运行Python脚本时传入参数的三种方式详解
2021-11-30 00:16:08
模糊查询的通用存储过程
2024-01-23 06:49:12
go语言实现二叉树的序例化与反序列化
2024-04-26 17:27:30
![](https://img.aspxhome.com/file/2023/2/134082_0s.webp)
python字典key不能是可以是啥类型
2022-12-28 20:54:04
详解python __init__.py 和 __all__作用
2023-08-22 06:55:22
![](https://img.aspxhome.com/file/2023/9/86589_0s.png)
在Django框架中设置语言偏好的教程
2022-01-18 22:47:35
微信小程序实现吸顶盒效果
2024-04-28 09:33:38
![](https://img.aspxhome.com/file/2023/6/132886_0s.jpg)
jQuery 1.4新特性及其变化(上)
2010-01-18 16:33:00
Python tkinter 列表框Listbox属性详情
2023-09-16 14:06:26
![](https://img.aspxhome.com/file/2023/7/100947_0s.png)
python编写俄罗斯方块
2023-04-02 11:03:06
CentOS7.5 安装 Mysql8.0.19的教程图文详解
2024-01-13 07:28:26
![](https://img.aspxhome.com/file/2023/7/128027_0s.png)
免费开源百度编辑器(UEditor)使用方法
2023-06-01 18:05:08
![](https://img.aspxhome.com/file/2023/8/70618_0s.jpg)
给Notepad++换主题
2009-05-04 14:43:00
![](https://img.aspxhome.com/file/UploadPic/20095/4/rhtml-39s.gif)
js倒计时代码
2008-05-07 13:41:00