Python爬取王者荣耀英雄图片

Python爬取王者荣耀英雄图片

爬虫,下图片,很简单,直接上代码

import requests
import os
import json


def get_hero_id():
    idlist = []
    namelist = []
    try:
        response = json.loads(requests.get('https://pvp.qq.com/web201605/js/herolist.json').content.decode())
        for item in response:
            idlist.append(item['ename'])
            namelist.append(item['cname'])
        return idlist,namelist
    except:
        pass
namelist,idlist = get_hero_id()[1],get_hero_id()[0]

def make_photo_url():
    urls = []
    for id in idlist:
        url = []
        for i in range(1,11):
            url.append('http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/%d/%d-bigskin-%d.jpg'%(id,id,i))
        urls.append(url)
    return urls
photo_urls = make_photo_url()

def matching():
    return dict(zip(namelist,photo_urls))
result = matching()

def download():
    if not os.path.exists('./王者图片'):
        os.mkdir('./王者图片')
    for item in namelist:
        if not os.path.exists('./王者图片/%s'%item):
            os.mkdir('./王者图片/%s'%item)
        urls = result[item]
        for i in range(10):
            url = urls[i]
            code = requests.get(url).status_code
            if code == 200:
                response = requests.get(url).content
                file = open('./王者图片/%s/%s%s.jpg'%(item,item,i+1),'wb')
                file.write(response)
                file.close()
                print("%s%d 下载完成"%(item,i+1))
            else:
                pass

if __name__ == '__main__':
    print("开始下载")
    download()
    print("下载完成")

你可能感兴趣的:(Python爬取王者荣耀英雄图片)