python爬虫王者荣耀高清壁纸

# run.py
import re, os, time, json, requests
from urllib.parse import unquote
import concurrent.futures as cf


class WangzheDownloader(object):
    # 初始化
    def __init__(self):
        self.root = './heros/'
        if not os.path.exists(self.root):
            os.mkdir(self.root)
        self.numberSize = 6
        self.url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&SearchKey=sProdName&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=1607443031450'
        res = requests.get(self.url.format(0)).json()
        self.totalPage = int(res.get('iTotalPages'))
        self.sum = int(res.get('iTotalLines'))
        self.count = 0
        self.startTime = time.time()

    # 选择壁纸分辨率
    def size(self):
        print('壁纸分辨率及序号如下: ')
        size_list = [
            '1 > 缩略图',
            '2 > 1024x768',
            '3 > 1280x720',
            '4 > 1280x1024',
            '5 > 1440x900',
            '6 > 1920x1080 (推荐)',
            '7 > 1920x1200',
            '8 > 1920x1440'
        ]
        for size in size_list:
            print(size)
        numberSize = input('请选择分辨率 (默认是6): ')
        self.numberSize = numberSize if numberSize else 6

    # 获取103位英雄的名字
    def name(self):
        url = 'https://pvp.qq.com/web201605/js/herolist.json'
        res = requests.get(url).json()
        self.hero = [item['cname'] for item in res]

    # 单张图片下载
    def down(self, img_name, img_url):
        res = requests.get(img_url)
        with open(img_name, 'wb') as f:
            f.write(res.content)
        self.count += 1
        endTime = time.time()
        self.show(self.count, self.sum, endTime-self.startTime)

    # 爬取每一页
    def page(self, numberPage):
        res = requests.get(self.url.format(numberPage)).json()
        for item in res.get('List'):
            # 去除图片名中的特殊字符
            img_name = re.sub('[/\\:*?"<>|]', '', unquote(item['sProdName']))

            # 根据英雄名建立分类文件夹
            temp = re.split('[·-]', img_name)
            control = True
            for each in temp:
                if each in self.hero:
                    control = False
                    img_dir = self.root+each
                    if not os.path.exists(img_dir):
                        os.mkdir(img_dir)
                    img_name = img_dir+'/'+img_name+'.jpg'
                    break
            if control:
                img_dir = self.root+'其它'
                if not os.path.exists(img_dir):
                    os.mkdir(img_dir)
                img_name = img_dir+'/'+img_name+'.jpg'
            
            img_url = unquote(item[f'sProdImgNo_{self.numberSize}'])
            img_url = img_url.replace('/200', '/0') # /0才是原图,/200都是缩略图
            self.down(img_name, img_url)

    # 打印进度条
    def show(self, num, _sum,  runTime):
        barLen = 20  # 进度条的长度
        perFin = num/_sum
        numFin = round(barLen*perFin)
        numNon = barLen-numFin
        leftTime = (1-perFin)*(runTime/perFin)
        print(
            f"{num:0>{len(str(_sum))}}/{_sum}",
            f"|{'█'*numFin}{' '*numNon}|",
            f"任务进度: {perFin*100:.0f}%",
            f"已用时间: {runTime:.0f}S",
            f"剩余时间: {leftTime:.0f}S",
            end='\r'
        )
        if num == _sum:
            print()

    # 多线程
    def main(self):
        self.size()
        self.name()
        with cf.ThreadPoolExecutor() as tp:
            for page in range(self.totalPage):
                tp.submit(self.page, page)


if __name__ == "__main__":
    WangzheDownloader().main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值