gpt4 book ai didi

python - Feedparser - KeyError : 'fullcount'

转载 作者:行者123 更新时间:2023-12-01 05:48:45 24 4
gpt4 key购买 nike

我尝试关注 this guide 。这是关于制作一个物理 Gmail 通知程序。当我输入相同的代码时,它发现一个错误:

Traceback (most recent call last):
File "C:/Python27/Projects/gmailnotifier.py", line 20, in <module>
)["feed"]["fullcount"])
File "C:\Python27\lib\site-packages\feedparser-5.1.3-py2.7.egg\feedparser.py", line 375, in __getitem__
return dict.__getitem__(self, key)
KeyError: 'fullcount'

我不知道为什么,这就是我问的原因。我使用的是 windows 7、python 2.7.3、feedparser 5.1.3 和 pyserial 2.6

完整代码如下:

import serial, sys, feedparser

#Settings - Change these to match your account details
USERNAME="mymain@gmail.com"
PASSWORD="mypassword"
PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom"

SERIALPORT = "COM3" # Change this to your serial port!

# Set up serial port
try:
ser = serial.Serial(SERIALPORT, 9600)
except serial.SerialException:
sys.exit()

newmails = int(feedparser.parse(
PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH
)["feed"]["fullcount"])

# Output data to serial port
if newmails > 0: ser.write('M')
else: ser.write('N')

# Close serial port
ser.close()

最佳答案

刚刚从 REPL 中看到了这一点。该代码对我来说将按原样工作。但是,我通过输入不正确的密码重现了您的错误。

如果身份验证失败,这就是 feedparser.parse()['feed'] 的样子:

>>> feedparser.parse(PROTO + USERNAME + ":" + INCORRECT_PASSWORD + "@" + SERVER + PATH)['feed']
{'summary': u'<h1>Unauthorized</h1>\n<h2>Error 401</h2>'}
>>>

如果您进行了正确的身份验证,它应该是这样的:

>>> import pprint
>>> pprint.pprint(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)['feed'])
{'fullcount': u'0',
'link': u'http://mail.google.com/mail',
'links': [{'href': u'http://mail.google.com/mail',
'rel': u'alternate',
'type': u'text/html'}],
'subtitle': u'New messages in your Gmail Inbox',
'subtitle_detail': {'base': u'https://mail.google.com/mail/feed/atom',
'language': None,
'type': u'text/plain',
'value': u'New messages in your Gmail Inbox'},
'title': u'Gmail - Inbox for xxxxxxx@gmail.com',
'title_detail': {'base': u'https://mail.google.com/mail/feed/atom',
'language': None,
'type': u'text/plain',
'value': u'Gmail - Inbox for xxxxxxx@gmail.com'},
'updated': u'2013-03-01T20:11:03Z',
'updated_parsed': time.struct_time(tm_year=2013, tm_mon=3, tm_mday=1, tm_hour=20, tm_min=11, tm_sec=3, tm_wday=4, tm_yday=60, tm_isdst=0)}
>>>

您应该打印出 feedparser.parse() 的结果来检查这是否确实是您遇到的问题,但我怀疑只需确保您的用户名/密码正确即可解决您的问题

关于python - Feedparser - KeyError : 'fullcount' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com