如何从json站点中排除if和alse语句中的keyerror



我正在抓取text.json站点以获取信息,有时我监视的元素会消失,因为它们不需要在那里。这会使程序停止并且无法重新启动,因为它们已经不在了。我需要能够,除非他们不在那里,并继续打印/发送正确的信息。

我试着用except KeyError:做一些事情,但似乎我做得不对。

如果有人能帮我,那就太棒了!注意:我去掉了端点!

特别是不总是显示的元素是PIDReleaseTypeTime

def check_endpoint():
endpoint = ""
req = requests.get(endpoint)
reqJson = json.loads(req.text)
for id in reqJson['threads']:  # For each id in threads list
PID = id['product']['globalPid']  # Get current PID
if PID in list:
print('checking for new products')
else:
title = (id['product']['title'])    
Image = (id['product']['imageUrl'])
ReleaseType = (id['product']['selectionEngine'])
Time = (id['product']['effectiveInStockStartSellDate'])
send(title, PID, Image, ReleaseType, Time)
print ('added to database'.format(PID))
list.append(PID)  # Add PID to the list
return

如果我现在按原样运行代码,我会得到当前的错误。这是我想排除的元素。

Traceback (most recent call last):
File "C:UsersDesktopFinal.py", line 89, in 
<module>
main()
File "C:UsersDesktopFinal.py", line 84, in 
main
check_endpoint()
File "C:UsersDesktopFinal.py", line 74, in 
check_endpoint
ReleaseType = (id['product']['selectionEngine'])
KeyError: 'selectionEngine'

那么你想要这样的东西(请更改list变量的名称,请参阅注释(

def check_endpoint():
endpoint = ""
req = requests.get(endpoint)
reqJson = json.loads(req.text)
for id in reqJson['threads']:  # For each id in threads list
PID = id['product']['globalPid']  # Get current PID
if PID in list:
print('checking for new products')
else:
try:
title = (id['product']['title'])    
Image = (id['product']['imageUrl'])
ReleaseType = (id['product']['selectionEngine'])
Time = (id['product']['effectiveInStockStartSellDate'])
except KeyError as e:
print("... ", e)
else:
# When all OK ...
send(title, PID, Image, ReleaseType, Time)
print ('added to database: {}'.format(PID))
list.append(PID)  # Add PID to the list

你希望它精确到什么程度取决于你自己。你可以用不同的方式处理事情。

在Python中处理KeyError响应的一个好方法是在字典上使用.get()方法。如果你调用get方法,你可以提供一个默认值,如果字典中不存在密钥:

>>> d = {'hi': 'there'}
>>> d.get('hi', 'cats') # return 'cats' if 'hi' is missing
'there'
>>> d.get('apples', 'cats') # return 'cats' if 'apple' is missing
'cats'

如果您有嵌套字典,您可以将{}设置为从一个字典提供的默认值,这样您就可以在每个子字典上继续调用.get()

>>> d = {}
>>> d['a'] = {}
>>> d['a']['b'] = 'c'
>>> d.get('a', {}).get('b', 'cats')
'c'
>>> d.get('x', {}).get('y', 'cats')
'cats'

您可以使用dict类型的.get(key[, default])方法(请参阅此处的文档(并设置默认值,而不是从带有方括号的字典中获取值。例如:

id['product'].get('selectionEngine', None)

如果id['product']具有密钥'selectionEngine',则这将给出id['product']['selectionEngine'],否则它将给出None。当然,您可以将None更改为对您的应用程序更有意义的其他值。

最新更新