-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
wzry.py
89 lines (78 loc) · 2.83 KB
/
wzry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
抓取王者荣耀皮肤
author: gxcuizy
date: 2018-11-06
"""
import requests
from bs4 import BeautifulSoup
from urllib import parse
import os
class Skin(object):
def __init__(self):
# 英雄的json数据
self.hero_url = 'https://pvp.qq.com/web201605/js/herolist.json'
# 英雄详细页的通用url前缀信息
self.base_url = 'https://pvp.qq.com/web201605/herodetail/'
# 英雄详细页url后缀信息
self.detail_url = ''
# 图片存储文件夹
self.img_folder = 'skin'
# 图片url的通用前缀
self.skin_url = 'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'
# 图片url的后缀信息
self.skin_detail_url = ''
def get_hero(self):
"""获取英雄的json数据"""
request = requests.get(self.hero_url)
hero_list = request.json()
return hero_list
def get_hero_skin(self, hero_name, hero_no):
"""获取详细页英雄皮肤展示的信息,并爬图"""
url = parse.urljoin(self.base_url, self.detail_url)
request = requests.get(url)
request.encoding = 'gbk'
html = request.text
# 获取皮肤信息的节点
soup = BeautifulSoup(html, 'lxml')
skip_list = soup.select('.pic-pf-list3')
for skin_info in skip_list:
# 获取皮肤名称
img_names = skin_info.attrs['data-imgname']
name_list = img_names.split('|')
skin_no = 1
# 循环下载皮肤图片
for skin_name in name_list:
self.skin_detail_url = '%s/%s-bigskin-%s.jpg' % (hero_no, hero_no, skin_no)
skin_no += 1
img_name = hero_name + '-' + skin_name + '.jpg'
self.download_skin(img_name)
def download_skin(self, img_name):
"""下载皮肤图片"""
img_url = parse.urljoin(self.skin_url, self.skin_detail_url)
request = requests.get(img_url)
if request.status_code == 200:
print('download-%s' % img_name)
img_path = os.path.join(self.img_folder, img_name)
with open(img_path, 'wb') as img:
img.write(request.content)
else:
print('img error!')
def make_folder(self):
"""创建图片存储文件夹"""
if not os.path.exists(self.img_folder):
os.mkdir(self.img_folder)
def run(self):
"""脚本执行入口"""
self.make_folder()
hero_list = self.get_hero()
for hero in hero_list:
hero_no = str(hero['ename'])
self.detail_url = hero_no + '.shtml'
hero_name = hero['cname']
self.get_hero_skin(hero_name, hero_no)
# 程序执行入口
if __name__ == '__main__':
skin = Skin()
skin.run()