代码
import urllib.request
import json,sys,os
'''
函数功能 - 使用进度条
parameter - 下载的条数
'''
m=0
k=0
def down_progress(hero_num):global mglobal kif k!=0 and m<=99 and k%(int)(hero_num/100)==0:m+=1print("\r", end="")print("Download progress: {}%: ".format(m), "▋" * (m // 2), end="")sys.stdout.flush()k+=1'''
函数功能 - 下载
parameter - 无
'''
def download(): response = urllib.request.urlopen("http://pvp.qq.com/web201605/js/herolist.json")hero_json = json.loads(response.read())hero_num = len(hero_json)save_dir = r'C:\Users\DXG\爬虫\爬虫案例\heroskin\\'if not os.path.exists(save_dir):os.mkdir(save_dir)for i in range(hero_num):down_progress(hero_num)hero_name = hero_json[i]['cname']hero_dir = save_dir+hero_name+'\\'if not os.path.exists(hero_dir):os.mkdir(hero_dir)if 'skin_name' in hero_json[i].keys():skin_names = hero_json[i]['skin_name'].split('|')else:skin_names = hero_json[i]['title'].split('|')for cnt in range(len(skin_names)):save_file_name = hero_dir + skin_names[cnt] + '.jpg'skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'+str(hero_json[i]['ename'])+ '/' +str(hero_json[i]['ename'])+'-bigskin-' + str(cnt+1) +'.jpg'if not os.path.exists(save_file_name):urllib.request.urlretrieve(skin_url, save_file_name)
if __name__ == '__main__':download()