文章目录
- Python爬虫——王者荣耀全皮肤拉取
- 资源地址
- HeroScrapy.py
- 运行上面的代码即可
- 注意点
Python爬虫——王者荣耀全皮肤拉取
开门见山,话不多说
资源地址
英雄信息列表:http://pvp.qq.com/web201605/js/herolist.json
皮肤前缀地址:http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info
HeroScrapy.py
import
requests
import
os
# 英雄信息列表
hero_url
=
'http://pvp.qq.com/web201605/js/herolist.json'
# 英雄的皮肤前缀地址
skin_url_perfix
=
'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'
# 当前文件的绝对路径
abs_path
=
os
.
path
.
abspath
(
'.'
)
# 获取所有英雄信息
head
=
{
'User-Agent'
:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
}
response
=
requests
.
get
(
hero_url
,
headers
=
head
)
hero_list
=
response
.
json
(
)
# 循环每个英雄
for
i
in
range
(
len
(
hero_list
)
)
:
if
"ename"
in
hero_list
[
i
]
and
"cname"
in
hero_list
[
i
]
and
"skin_name"
in
hero_list
[
i
]
:
ename
=
hero_list
[
i
]
[
'ename'
]
# 英雄编号
cname
=
hero_list
[
i
]
[
'cname'
]
# 英雄名
skin_name
=
hero_list
[
i
]
[
'skin_name'
]
.
split
(
'|'
)
# 该英雄所有皮肤名
skinCount
=
len
(
skin_name
)
# 皮肤数量
print
(
str
(
cname
)
+
"的皮肤数量为:"
+
str
(
skinCount
)
)
# 循环下载每个皮肤
for
j
in
range
(
1
,
skinCount
+
1
)
:
skin_url
=
skin_url_perfix
+
str
(
ename
)
+
'/'
+
str
(
ename
)
+
'-bigskin-'
+
str
(
j
)
+
'.jpg'
skin_picture
=
requests
.
get
(
skin_url
)
.
content
# 创建文件路径
fileDir
=
abs_path
+
"\\HeroSkins\\"
+
str
(
cname
)
+
"\\"
if
not
os
.
path
.
exists
(
fileDir
)
:
os
.
makedirs
(
fileDir
)
# 文件写入
with
open
(
fileDir
+
skin_name
[
j
-
1
]
+
'.jpg'
,
'wb'
)
as
f
:
f
.
write
(
skin_picture
)
运行上面的代码即可
拉取的图片会放到代码当前文件的目录下
注意点
1.requests 模块需要安装
pip install requests
2.注意代码的严谨性
if
"ename"
in
hero_list
[
i
]
and
"cname"
in
hero_list
[
i
]
and
"skin_name"
in
hero_list
[
i
]
:
这个是要判断的,为啥?看下英雄信息列表:
存在部分英雄是没有skin_name,不判空程序没跑完就GG了