Python KeyError:';全数';邮件脚本

Python KeyError:';全数';邮件脚本,python,parsing,email,raspberry-pi,keyerror,Python,Parsing,Email,Raspberry Pi,Keyerror,我编写了一个Python脚本来检查我的电子邮件,当我收到新邮件时,打开一个LED。 大约1小时后,我得到了错误: Traceback (most recent call last): File "checkmail.py", line 10, in <module> B = int(feedparser.parse("https://" + U + ":" + P + "@mail.google.com/gmail/feed/atom")["feed"]["fullcoun

我编写了一个Python脚本来检查我的电子邮件,当我收到新邮件时,打开一个LED。 大约1小时后,我得到了错误:

Traceback (most recent call last):
  File "checkmail.py", line 10, in <module>
   B = int(feedparser.parse("https://" + U + ":" + P + "@mail.google.com/gmail/feed/atom")["feed"]["fullcount"])
  File "/usr/local/lib/python2.7/dist-packages/feedparser.py", line 375, in __getitem__
    return dict.__getitem__(self, key)
KeyError: 'fullcount'
我用树莓皮做的。
提前感谢您的帮助。

您需要添加一些调试代码,并查看此调用返回的内容:

feedparser.parse("https://" + U + ":" + P + "@mail.google.com/gmail/feed/atom")["feed"]
它显然不包含“完整计数”项。您可能希望执行以下操作:

feed = feedparser.parse("https://{}:{}@mail.google.com/gmail/feed/atom".format(U, P))
try:
    B = int(feed["feed"]["fullcount"])
except KeyError:
    # handle the error
    continue  # you might want to sleep or put the following code in the else block
通过这种方式,您可以处理错误(如果
int()
由于无效值而失败,您可能也希望捕获
ValueError
),而不会使脚本崩溃

feed = feedparser.parse("https://{}:{}@mail.google.com/gmail/feed/atom".format(U, P))
try:
    B = int(feed["feed"]["fullcount"])
except KeyError:
    # handle the error
    continue  # you might want to sleep or put the following code in the else block