无法通过API对过去的1000条评论进行分页

时间:2012-11-02 03:24:49

标签: python facebook-graph-api

我的Facebook帖子至少有4,000条公开评论。但我不能得到超过980个。

每次,当我清除900时,我都会收到此错误:

Traceback (most recent call last):
    File "fb_pcf_temp.py", line 41, in <module>
    next = decoded['paging']['next']
KeyError: 'next'

我的代码是:

#!/usr/bin/env python
# encoding: utf-8
"""
postgrabber.py

Created by ian on 2012-09-24.
Copyright (c) 2012 __MyCompanyName__. All rights reserved.
"""

import sys
import os
import ogp
import config
import httplib
import simplejson as json
import requests

getData = ogp.facebookQueries()
post_id = "145061248901557_381440708596942"

def getmore(nexturl):
    access_token = getData.authenticate()
    theFeedUrl = nexturl + "&" + access_token
    print theFeedUrl
    req = requests.get(theFeedUrl)
    f = req.text
    decoded = json.loads(f)
    return decoded

next = "https://graph.facebook.com/%s?fields=comments.limit(100).fields(likes,message,from,like_count)" % post_id
x = 0
while x < 40:
    decoded = getmore(next)
    try:
        comments = decoded['comments']['data']
        next = decoded['comments']['paging']['next']

    except:
        comments = decoded['data']
        next = decoded['paging']['next']

    for d in comments:
        print '%s\t%s\t%s\t%s\t"%s"' % (d['created_time'],d['like_count'],d['from']['name'],d['from']['id'],d['message'])
    x = x + 1

我很肯定我已经收到了1000多条公众意见。该帖子总共有4780条评论 - 我认为不太可能有3800条评论是私密的。

还有其他人遇到过这个吗?我做了一件明显不对的事吗?

1 个答案:

答案 0 :(得分:0)

我发现了问题。令人惊讶的是如何让一群其他人突然露出脸庞。

我有这个:

    theFeedUrl = nexturl + "&" + access_token

应该是:

    theFeedUrl = nexturl + "&access_token=" + access_token

愚蠢的粗心错误,由于某种原因,Facebook让我请求前1000个正确的URL,这一事实进一步加剧了这一点。