Python Facebook API - cursor pagination

2024/10/7 16:25:46

My question involves learning how to retrieve my entire list of friends using Facebook's Python API. The current result returns an object with limited number of friends and a link to the 'next' page. How do I use this to fetch the next set of friends ? (Please post the link to possible duplicates) Any help would be much appreciated. In general, I need to learn about the pagination involved the API usage.

import facebook
import jsonACCESS_TOKEN = "my_token"g = facebook.GraphAPI(ACCESS_TOKEN)print json.dumps(g.get_connections("me","friends"),indent=1)
Answer

Sadly the documentation of pagination is an open issue since almost 2 years. You should be able to paginate like this (based on this example) using requests:

import facebook
import requestsACCESS_TOKEN = "my_token"
graph = facebook.GraphAPI(ACCESS_TOKEN)
friends = graph.get_connections("me","friends")allfriends = []# Wrap this block in a while loop so we can keep paginating requests until
# finished.
while(True):try:for friend in friends['data']:allfriends.append(friend['name'].encode('utf-8'))# Attempt to make a request to the next page of data, if it exists.friends=requests.get(friends['paging']['next']).json()except KeyError:# When there are no more pages (['paging']['next']), break from the# loop and end the script.break
print allfriends

Update: There's a new generator method available which implements above behavior and can be used to iterate over all friends like this:

for friend in graph.get_all_connections("me", "friends"):# Do something with this friend.
https://en.xdnf.cn/q/70219.html

Related Q&A

PyQt Irregularly Shaped Windows (e.g. A circular without a border/decorations)

How do I create an irregularly shaped window in PyQt?I found this C++ solution, however I am unsure of how to do that in Python.

default values for variable argument list in Python

Is it possible to set a default value for a variable argument list in Python 3?Something like:def do_it(*args=(2, 5, 21)):passI wonder that a variable argument list is of type tuple but no tuple is ac…

Python error: execute cannot be used while an asynchronous query is underway

How do I prevent the error “ProgrammingError: execute cannot be used while an asynchronous query is underway”? From the docs it says that I should use psycopg2.extras.wait_select if I’m using a cor…

Clearing Django form fields on form validation error?

I have a Django form that allows a user to change their password. I find it confusing on form error for the fields to have the *ed out data still in them.Ive tried several methods for removing form.dat…

How to watch xvfb session thats inside a docker on remote server from my local browser?

Im running a docker (That I built on my own), thats docker running E2E tests. The browser is up and running but I want to have another nice to have feature, I want the ability of watching the session o…

Flask WSGI application hangs when import nltk

I followed the instructions here to create a onefile flask-app deployed to apache2 with mod-wsgi on ubuntu. That all works fine when using the original flask app. However, when adding import nltk to th…

python append folder name to filenames in all sub folders

I am trying to append the name of a folder to all filenames within that folder. I have to loop through a parent folder that contain sub folders. I have to do this in Python and not a bat file.Example i…

When ruamel.yaml loads @dataclass from string, __post_init__ is not called

Assume I created a @dataclass class Foo, and added a __post_init__ to perform type checking and processing.When I attempt to yaml.load a !Foo object, __post_init__ is not called.from dataclasses import…

How is the python module search path determined on Mac OS X?

When a non built-in module is imported, the interpreter searches in the locations given by sys.path. sys.path is initialized from these locations (http://docs.python.org/library/sys.html#sys.path):the …

Apply Mask Array 2d to 3d

I want to apply a mask of 2 dimensions (an NxM array) to a 3 dimensional array (a KxNxM array). How can I do this?2d = lat x lon 3d = time x lat x lonimport numpy as npa = np.array([[[ 0, 1, 2],[ 3,…