王者英雄都非常绚丽夺目,下面就是爬取壁纸的源码:
import requests
import os
import json
from lxml import etree
from fake_useragent import UserAgent
import logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')class glory_of_king(object):def __init__(self):if not os.path.exists("./王者荣耀皮肤"):os.mkdir("王者荣耀皮肤")ua = UserAgent(verify_ssl=False, path='fake_useragent.json')for i in range(1, 50):self.headers = {'User-Agent': ua.random}def scrape_skin(self):response = requests.get('https://pvp.qq.com/web201605/js/herolist.json', headers=self.headers)data = json.loads(response.text)for i in data:hero_number = i['ename'] hero_name = i['cname'] os.mkdir("./王者荣耀皮肤/{}".format(hero_name)) response_src = requests.get("https://pvp.qq.com/web201605/herodetail/{}.shtml".format(hero_number),headers=self.headers)hero_content = response_src.content.decode('gbk') hero_data = etree.HTML(hero_content)hero_img = hero_data.xpath('//div[@class="pic-pf"]/ul/@data-imgname')hero_src = hero_img[0].split('|')logging.info(hero_src)for j in range(len(hero_src)):index_ = hero_src[j].find("&")skin_name = hero_src[j][:index_]response_skin = requests.get("https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{}/{}-bigskin-{}.jpg".format(hero_number, hero_number, j + 1)) skin_img = response_skin.content with open("./王者荣耀皮肤/{}/{}.jpg".format(hero_name, skin_name), "wb")as f:f.write(skin_img)logging.info(f"{skin_name}.jpg 下载成功!!")def run(self):self.scrape_skin()if __name__ == '__main__':spider = glory_of_king()spider.run()