Home > other >  The keyerror is what problem?
The keyerror is what problem?

Time:11-27

The dictionary is the key, after running the program error "keyerror
"
The import requests
The import json
The import OS
Url="https://pvp.qq.com/web201605/js/herolist.json"
Re=requests. Get (url)
JsonFile=re. Json ()
The for m in range (len (jsonFile) :

# digital name
Ename=jsonFile [m] [' ename]
# Chinese name
Cname=jsonFile [m] [' cname]
# after cutting is dictionary list form
SkinName=jsonFile [m] [r]. "skin_name" split (' | ')
# calculating how much each hero skin
SkinNumber=len (skinName)
Print (" {0}, {1} {2} "skin. The format (cname, skinNumber skinName))
# the cycle is to download the image construction site
For bigskin in range (1, skinNumber + 1) : # 1 - skinNumber
# image url specified
UrlPicture='http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + STR (ename) + '/' + STR (
Ename) + '- bigskin -' + STR (bigskin) + 'JPG'
# get image information are binary content is secondary system information
Picture=requests. Get (urlPicture). The content
Save the file # save information
If not OS. Path. The exists (OS. Path. The split (' d:/hero/) [0]) :
# directory does not exist to create, makedirs can create multi-level directory
OS. Makedirs (OS. Path. The split (' d:/hero/) [0])
With the open (' d: \ \ hero \ \ '+ cname + skinName [bigskin - 1) +' JPG ', 'wb) as f:
F.w rite (picture)
Print (' * '* 50)
All the pictures of print (' to download is completed, the default in d:/hero/')
Input (' please press the return key to help you open the folder... ')
OS. Startfile (' d:/hero/')
Input (' please press enter to exit... ')

CodePudding user response:

Brother collection, a dictionary is not such use,
If your jsonFile is a dictionary type, such as jsonFile={' ename ':' XXX '} can be directly use jsonFile [' ename] is the values in the dictionary of XXX, if jsonFile is a list, the list is saved in a dictionary type of value that can be with you in the code method to access, such as jsonFile=[{' ename ':' XXX '}, {' ename ':' XXXX '}], then can use jsonFile [m] [' cname] to values,
It might be, but I guess your jsonFile of {data: [{" ename ":" XXX "}, {" ename ":" XXX "}, {" ename ":" XXX "}]}
This kind of situation, you can visit in the following way:
 
Data=https://bbs.csdn.net/topics/jsonFile (' data ')
For d in data:
Ename=d [' ename]

CodePudding user response:

Just I also climbed the skin of the glory of the king, the problem here is, herolist. Json, this is not a complete file returns json, among them, d the hero of the object, without a skin_name, cause the program run to d this object, cannot resolve the keyword skin_name,
So here goes to programming to consider a big point: application robustness, robustness, one of the key elements of the input legality judgment, cannot be blamed on herolist. Json is wrong, because this is the external input, there will always be inevitable incompatibility, need to call herolist. The related fields in json, judge, avoid the program crashes,

The json data is not complete, part of the skin of the hero is not all, of course this does not affect the program run,

CodePudding user response:

Is, d this without this parameter, it is a question of tencent!!!!
  • Related