作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
OK,我有这段代码,我从instagram的api中获取了一些json...。
instaINFO = requests.get("https://api.instagram.com/v1/media/%s?access_token=xyz" % instaMeID).json()
print instaINFO
#pdb.set_trace()
MSG = {'fromEMAIL': uEmail, 'toCHANNELID': channelID, 'timeSENT': uTi me, 'profilePIC': instaINFO['data']['user']['profile_picture'],'userNAME': instaINFO[ 'data']['user']['username'], 'msgBODY': instaINFO['data']['caption']['text']}
instaINFO['data']['caption']['text']
MSG = {'fromEMAIL': uEmail, 'toCHANNELID': channelID, 'timeSENT': uTime,
'profilePIC': instaINFO['data']['user']['profile_picture'],'userNAME':
instaINFO['data']['user']['username'], 'msgBODY': instaINFO['data']['caption']
['text']}
TypeError: 'NoneType' object is not subscriptable
if instaINFO['data']['caption']['text'] == None:
pass
最佳答案
如果要尽可能多地填充MSG
字典,则需要分别添加每个值:
MSG = {'fromEMAIL': uEmail, 'toCHANNELID': channelID, 'timeSENT': uTime}
try:
MSG['profilePIC'] = instaINFO['data']['user']['profile_picture']
except TypeError:
MSG['profilePIC'] = ""
try:
MSG['userNAME'] = instaINFO['data']['user']['username']
except TypeError:
MSG['userNAME'] = ""
try:
MSG['msgBODY'] = instaINFO['data']['caption']['text']
except TypeError:
MSG['msgBODY'] = ""
MSG = {'fromEMAIL': uEmail, 'toCHANNELID': channelID, 'timeSENT': uTime}
for mkey, subdict, ikey in (('profilePIC', 'user', 'profile_picture'),
('userNAME', 'user', 'username'),
('msgBODY', 'cpation', 'text')):
try:
MSG[msgkey] = instaINFO['data'][subdict][instakey]
except TypeError:
MSG[msgkey] = ""
关于python - 如何在Python中正确错误检查JSON值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18097439/
我是一名优秀的程序员,十分优秀!