def hero_skin(): import json import os import requests from bs4 import BeautifulSoup headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'} hero_prefix = 'https://pvp.qq.com/web201605/herodetail/' skin_prefix = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' def get_heroes(): data_url = 'https://pvp.qq.com/web201605/js/herolist.json' r = requests.get(data_url, headers) return json.loads(r.text) def process_hero(hero): hero_id = str(hero['ename']) hero_name = hero['cname'] hero_path = os.path.join("皮肤", hero_name) if not os.path.isdir(hero_path): os.mkdir(hero_path) hero_url = hero_prefix + hero_id + '.shtml' r = requests.get(hero_url, headers) text = r.text.encode('iso-8859-1').decode('gbk') soup = BeautifulSoup(text, 'lxml') skins = soup.select('ul.pic-pf-list.pic-pf-list3')[0].get('data-imgname').split('|') for i in range(len(skins)): # 页面信息是动态加载出来得,不能直接提取,需要拼装(下载大图) img_url = skin_prefix + hero_id + '/' + hero_id + '-bigskin-' + str(i + 1) + '.jpg' r = requests.get(img_url, headers) with open(hero_path + '/' + skins[i] + '.jpg', 'wb+') as f: f.write(r.content) print(hero_name + '有' + str(len(skins)) + '张皮肤') for hero in get_heroes(): process_hero(hero) hero_skin()