Feedparser - KeyError: 'fullcount'

混江龙づ霸主 提交于 2019-12-14 02:43:50

问题


I have tried to follow this guide. It is about making a physical gmail notifier. When I entered the same code it found an error:

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'

I am not sure why and thats why im asking. I am using windows 7, python 2.7.3, feedparser 5.1.3 and pyserial 2.6

Here is the full code:

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()

回答1:


Just took a look at that from a REPL. The code will work as-is for me. However, I was able to reproduce your error by entering an incorrect password.

This is what feedparser.parse()['feed'] looks like if you fail to authenticate:

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

This is what it should look like if you do authenticate properly:

>>> 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)}
>>> 

You should print out the result of feedparser.parse() to check if this is indeed the problem you have, but I suspect that simply making sure your username/password are correct will fix your problem



来源:https://stackoverflow.com/questions/15164294/feedparser-keyerror-fullcount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!