代码
import re
import json
import os
from concurrent.futures import ThreadPoolExecutor
import requests
class WZRY:
folder = "英雄皮肤"
pool = ThreadPoolExecutor(max_workers=20)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
"referer": "https://pvp.qq.com/"
}
def __init__(self):
if not os.path.exists(self.folder):
os.mkdir(self.folder)
def parse(self, url):
try:
resp = requests.get(url, headers=self.headers)
assert resp.status_code == 200
return resp
except:
return None
def process_hero(self, hero_id, hero_name):
hero_dir = f"{self.folder}/{hero_name}"
if not os.path.exists(hero_dir):
os.mkdir(hero_dir)
for num in range(1, 16):
img_path = f"{hero_dir}/{hero_name}-{num}.png"
img_url = f"https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{hero_id}/{hero_id}-bigskin-{num}.jpg"
self.pool.submit(self.save_img, img_path, img_url)
def save_img(self, img_path, img_url):
resp = self.parse(img_url)
if resp is None:
return
if os.path.exists(img_path):
print(f"已下载过 ==> {img_path}")
return
with open(img_path, "wb") as f:
f.write(resp.content)
print(f"成功下载 ==> {img_path}")
def run(self):
url = "https://game.gtimg.cn/images/yxzj/web201706/js/heroid.js"
html = self.parse(url).text
rule = re.compile('var module_exports = ({.*?})', re.S)
result = rule.findall(html)[0]
hero_infos = re.sub("'", '"', result)
infos = json.loads(hero_infos)
for hero_id, hero_name in infos.items():
self.process_hero(hero_id, hero_name)
if __name__ == '__main__':
WZRY().run()
结果
|