本文介绍了Feedparser - KeyError: 'fullcount'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试遵循本指南.它是关于制作物理 gmail 通知程序.当我输入相同的代码时,它发现了一个错误:

回溯(最近一次调用最后一次):文件C:/Python27/Projects/gmailnotifier.py",第 20 行,在 <module> 中)["feed"]["fullcount"])文件C:\Python27\lib\site-packages\feedparser-5.1.3-py2.7.egg\feedparser.py",第 375 行,在 __getitem__ 中返回 dict.__getitem__(self, key)KeyError: '全数'

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

完整代码如下:

import serial, sys, feedparser#Settings - 更改这些以匹配您的帐户详细信息USERNAME="[email protected]"密码=我的密码"原型="https://"服务器="mail.google.com"路径="/gmail/feed/atom"SERIALPORT = "COM3" # 把这个改成你的串口!#设置串口尝试:ser = serial.Serial(SERIALPORT, 9600)除了serial.SerialException:系统退出()newmails = int(feedparser.parse(原型 + 用户名 + ":" + 密码 + "@" + 服务器 + 路径)["feed"]["fullcount"])# 输出数据到串口如果新邮件>0: ser.write('M')其他:ser.write('N')# 关闭串口ser.close()
解决方案

刚刚从 REPL 中看了一下.该代码将按原样对我工作.但是,我可以通过输入错误的密码来重现您的错误.

这是feedparser.parse()['feed'] 如果您无法进行身份验证的样子:

>>>feedparser.parse(PROTO + USERNAME + ":" + INCORRECT_PASSWORD + "@" + SERVER + PATH)['feed']{'summary': u'

Unauthorized

\n

Error 401

'}>>>

如果您正确进行身份验证,它应该是什么样子:

>>>导入打印>>>pprint.pprint(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)['feed']){'fullcount': u'0','链接':u'http://mail.google.com/mail','links': [{'href': u'http://mail.google.com/mail','rel': u'alternate','type': u'text/html'}],'subtitle': u'Gmail 收件箱中的新邮件','subtitle_detail': {'base': u'https://mail.google.com/mail/feed/atom','语言':无,'type': u'text/plain','value': u'Gmail 收件箱中的新邮件'},'title': u'Gmail - [email protected] 的收件箱','title_detail': {'base': u'https://mail.google.com/mail/feed/atom','语言':无,'type': u'text/plain','value': u'Gmail - [email protected] 的收件箱'},'更新':u'2013-03-01T20:11:03Z','updated_pa​​rsed': 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() 的结果以检查这是否确实是您遇到的问题,但我怀疑只需确保您的用户名/密码正确即可解决您的问题

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="[email protected]"
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()
解决方案

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 [email protected]',
 'title_detail': {'base': u'https://mail.google.com/mail/feed/atom',
                  'language': None,
                  'type': u'text/plain',
                  'value': u'Gmail - Inbox for [email protected]'},
 '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

这篇关于Feedparser - KeyError: 'fullcount'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 14:59