智一面的面试题提供python的测试题
使用地址:http://www.gtalent.cn/exam/interview?token=52cf92de494f4a8b6165d817a7279966
- 找到皮肤图片的地址
- 下载图片
1. 寻找皮肤图片地址1.1 找到英雄列表
英雄列表.png
# 代码片段1
import urllib.request
import json
import os
response = urllib.request.urlopen("http://pvp.qq.com/web201605/js/herolist.json")
hero_json = json.loads(response.read())
hero_num = len(hero_json)
print(hero_json)
print("hero_num : " , str(hero_num))
孙尚香皮肤.png
2. 下载图片2.1 英雄有几个皮肤
# 代码片段2
hero_name = hero_json[0]['cname']
skin_names = hero_json[0]['skin_name'].split('|')
skin_num = len(skin_names)
print('hero_name: ', hero_name)
print('skin_names :', skin_names)
print('skin_num: ' + str(skin_num))
2.2 下载文件
for i in range(hero_num):
# 获取皮肤名称列表
skin_names = hero_json['skin_name'].split('|')
for cnt in range(len(skin_names)):
save_file_name = 'D:\heroskin\\' + str(hero_json['ename']) + '-' +hero_json['cname']+ '-' +skin_names[cnt] + '.jpg'
skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'+str(hero_json['ename'])+ '/' +str(hero_json['ename'])+'-bigskin-' + str(cnt+1) +'.jpg'
urllib.request.urlretrieve(skin_url, save_file_name)
- 如果路径D:\herolist\不存在,则程序运行失败;
- 如果中途下载失败,再次运行程序的时候已经下载过的图片还会再下载一次。
-
检查文件是否存在,如果不存在则创建,代码如下:# 文件夹不存在则创建
save_dir = 'D:\heroskin'
if not os.path.exists(save_dir):
os.mkdir(save_dir) -
检查文件是否存在,如果存在则跳过下载,代码如下:if not os.path.exists(save_file_name):
urllib.request.urlretrieve(skin_url, save_file_name)
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 23 23:12:17 2017
@author: WangQiang
"""
import urllib.request
import json
import os
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 = 'D:\heroskin\\'
if not os.path.exists(save_dir):
os.mkdir(save_dir)
for i in range(hero_num):
# 获取英雄皮肤列表
skin_names = hero_json['skin_name'].split('|')
for cnt in range(len(skin_names)):
save_file_name = save_dir + str(hero_json['ename']) + '-' +hero_json['cname']+ '-' +skin_names[cnt] + '.jpg'
skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'+str(hero_json['ename'])+ '/' +str(hero_json['ename'])+'-bigskin-' + str(cnt+1) +'.jpg'
if not os.path.exists(save_file_name):
urllib.request.urlretrieve(skin_url, save_file_name)