简体   繁体   中英

How to get all comments of a facebook post using facebook SDK in Python?

I want to get all comments of a facebook post. We can extract comments by passing the limit() in Api call But how we know the limit? I need all comments.

https://graph.facebook.com/10153608167431961?fields=comments.limit(100).summary(true)&access_token=LMN  

By using this

data = graph.get_connections(id='10153608167431961', connection_name='comments')

I am getting few comments. How can I get all comments of a post?

Edit

import codecs
import json
import urllib

import requests
B = "https://graph.facebook.com/"
P ="10153608167431961"
f = "?fields=comments.limit(4).summary(true)&access_token="
T = "EAACb6lXwZDZD"
url = B+P+f+T


def fun(data):

    nextLink =  (data['comments']['paging']['next'])
    print(nextLink)
    for i in data['comments']['data']:
        print (i['message'])

    html = urllib.request.urlopen(nextLink).read()
    data = json.loads(html.decode('utf-8'))
    fun(data)

html = urllib.request.urlopen(url).read()
d = json.loads(html.decode('utf-8'))

fun(d)

It is giving the error

KeyError: 'comments'

You need to implement paging to get all/more comments: https://developers.facebook.com/docs/graph-api/using-graph-api/#paging

Usually, this is done with a "recursive function", but you have to keep in mind that you may hit API limits if there are a LOT of comments. It´s better to load more comments on demand only.

Example for JavaScript: How does paging in facebook javascript API work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM