0

I use fbconsole for authetication on Facebook.

import fbconsole
fbconsole.APP_ID = '1234567890'
fbconsole.AUTH_SCOPE = ['publish_stream', 'publish_checkins', 'read_stream',         'offline_access']
fbconsole.authenticate()
newsfeed = fbconsole.get('/me/home', {'fields':'from,name,description,message'})
newsfeedData = newsfeed["data"]
for status in newsfeedData:
    print status['from']['name'];
    print status['created_time'];
    #print status['name']
    print status['message']
    #print status.encode("utf-8")
    print('##############################################################################')

The status['from']['name'] shows name of friend, but status['message'] shows KeyError. How can I print news from Facebook, if 'status' is dictionary type and it HAS the 'message' key??? The same error is printing on 'name' key.

4

2 回答 2

0

有时没有“ name ”和“ message ”值,在这种情况下,facebook 不会将它们包含在响应中。这可能是它引发错误的原因。我想您应该检查密钥是否存在。但老实说,我不知道如何在 Python 中做到这一点。

于 2013-02-05T15:44:26.453 回答
0

您可以使用 python 的 (try, except) 块来解决问题。例如

try:
 print(status['message'])
except(KeyError):
 print("This post doesn't contain any message")
 continue
于 2013-12-07T07:28:52.557 回答