我目前正在尝试一些名为“#34; Programming Collective Intelligence"我遇到了pydelicious.py引起的这个错误。谁能告诉我如何调试这些问题?
Python代码:
def initializeUserDict(tag, count = 5):
user_dict = {}
#get the top count' popular posts
for p1 in get_popular(tag = tag)[0:count]:
#find all users who posted this
for p2 in get_urlposts(p1['href']):
user = p2['user']
user_dict[user] = {}
return user_dict
错误:
>>>from deliciousrec import *
>>>delusers = initializeUserDict('programming')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "deliciousrec.py", line 9, in initializeUserDict
for p2 in get_urlposts(p1['href']):
KeyError: 'href'
P1:
for p1 in get_popular(tag = 'python')[0:5]:
print p1
{'extended': u'', 'description': u'20.19. SimpleHTTPServer \u2014 Simple HTTP request handler \u2014 Python v2.7.7 documentation', 'tags': u'http', 'url': u'https://docs.python.org/2/library/simplehttpserver.html', 'user': u'', 'dt': ''}
{'extended': u'', 'description': u'Must Have Python Packages | Algorithm.co.il', 'tags': u'python', 'url': u'http://www.algorithm.co.il/blogs/programming/must-have-python-packages/', 'user': u'', 'dt': ''}
{'extended': u'', 'description': u'Le blog de Thomas Blanchard: Configure pydistutils.cfg - example python distutils config file', 'tags': u'distutils', 'url': u'http://bouktin.blogspot.be/2012/04/configure-pydistutilscfg-python.html', 'user': u'', 'dt': ''}
{'extended': u'', 'description': u'3.2 Installation', 'tags': u'cheetah', 'url': u'http://www.cheetahtemplate.org/docs/users_guide_html_multipage/gettingStarted.install.html', 'user': u'', 'dt': ''}
{'extended': u'', 'description': u"How can I represent an 'Enum' in Python? - Stack Overflow", 'tags': u'enum', 'url': u'http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python?rq=1', 'user': u'', 'dt': ''}
答案 0 :(得分:2)
不要使用&#34; href&#34;你需要改变&#34; href&#34;通过&#34; url&#34;
def initializeUserDict(tag, count=5):
user_dict = {}
# get the top count' popular posts
for p1 in get_popular(tag=tag)[0:count]:
# find all users who posted this
for p2 in get_urlposts(p1['url']): #<----------------HERE, I changed href by url
user = p2['user']
user_dict[user] = {}
return user_dict
最好的问候
答案 1 :(得分:0)
来自p1
的{{1}}似乎没有与密钥get_popular()
相关联的值。您应该为密钥测试'href'
,或者将p1
块放在try-catch中。