python爬取王者荣耀英雄素材图案例

import time
import requests
import threading
from queue import Queue
from urllib import parse
import os

# url_link1=[]#单图链列表
url_name=[]#文件夹名列表
url_link=[]#全图链列表
g_lock = threading.Lock()# 初始化一个锁

class Get_url():#获取url和html的内容
    def __init__(self,url,headers):
        self.url=url
        self.headers=headers

    def run(self):
        resp=requests.get(self.url,headers=self.headers)
        return resp.json()

class Get_link():#获取图链和图链名称
    def __init__(self,pin_url):
        self.pin_url=pin_url

    def run(self):
        global url_link
        # global url_link1
        true_url=self.pin_url['List']
        for true_url1 in true_url:
            url_link1=[]
            for i in range(1,9):
                true_url2=true_url1['sProdImgNo_{}'.format(i)]
                result1 = parse.unquote(true_url2).replace('/200','/0')
                url_link1.append(result1)
            url_link.append(url_link1)
        return url_link

class Get_name():#获取文件夹名称
    def __init__(self, pin_url):
        self.pin_url = pin_url

    def run(self):
        global url_name
        true_name = self.pin_url['List']
        for true_name1 in true_name:
            true_name2=true_name1['sProdName']
            result2=parse.unquote(true_name2)
            url_name.append(result2)
        return url_name


class Storage_pic(threading.Thread):# 4建立文件夹存储图片
    def run(self):
        headers = {
            'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36'}
        global url_link
        global url_name
        g_lock.acquire()
        for key,values1 in zip(url_name,url_link):
            print(key,values1)
            is_exists=os.path.exists('../王者荣耀案例/王者荣耀高清壁纸/'+key)
            if not is_exists:
                os.makedirs('../王者荣耀案例/王者荣耀高清壁纸/'+key)
                print(key+'目录创建成功')
            else:
                print(key+'目录已存在')

            m=1

            for i in range(len(values1)):
                if values1 !=[]:
                    values=values1.pop()
                    print('正在存储:'+values)
                    resp=requests.get(values,headers=headers)
                    with open('../王者荣耀案例/王者荣耀高清壁纸/'+key+'/'+str(m)+'.jpg','wb') as f:
                        f.write(resp.content)
                        m+=1
                else:print('该壁纸已经下载完毕!')
        g_lock.release()

def start():
    for i in range(1,2):
        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&iSortNumClose=1&jsoncallback=&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=1637683982984'.format(i)
        headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36'}
        # 1获取地址
        get_url=Get_url(url,headers)
        pin_url=get_url.run()
        # 2获取图链
        get_link=Get_link(pin_url)
        url_link=get_link.run()
        # 3获取文件夹名称
        get_name=Get_name(pin_url)
        url_name=get_name.run()
        # 4建立文件夹存储图片
        for x in range(10):
            storage_pic=Storage_pic()
            storage_pic.start()

if __name__ == '__main__':
    start()


#本案例只做学习使用,不做他用!

你可能感兴趣的:(python,算法,爬虫)