Python Facebook API - pagination cursor

My question includes learning how to get the entire list of friends using the Python Python API. The current result returns an object with a limited number of friends and a link to the next page. How to use this to get the next set of friends? (Please post a link to possible duplicates). Any help would be greatly appreciated. In general, I need to learn about pagination that uses the API.

import facebook import json ACCESS_TOKEN = "my_token" g = facebook.GraphAPI(ACCESS_TOKEN) print json.dumps(g.get_connections("me","friends"),indent=1) 
+6
source share
3 answers

Unfortunately, the pagination documentation is an open question since almost 2 years . You should be able to paginate pages (based on this example ) using queries :

 import facebook import requests ACCESS_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: A new generator method is available that implements the behavior described above and can be used to iterate over all friends, for example:

 for friend in graph.get_all_connections("me", "friends"): # Do something with this friend. 
+19
source

Meanwhile, I was looking for an answer, here is a much better approach:

 import facebook access_token = "" graph = facebook.GraphAPI(access_token = access_token) totalFriends = [] friends = graph.get_connections("me", "/friends&summary=1") while 'paging' in friends: for i in friends['data']: totalFriends.append(i['id']) friends = graph.get_connections("me", "/friends&summary=1&after=" + friends['paging']['cursors']['after']) 

At the endpoint, you will receive one answer in which the data will be empty, and then there will be no "paging" key, so at this time it will break and all the data will be saved.

0
source

in this example, you disabled installation / pagination one at a time, I think my while loop is simple, as it searches for the keyword β€œnext”, if it is not, if it does not exist, then we end the cycle and you will have the results in the list. in this example i'm just looking for all the people calling jacob

 import requests import facebook token = access_token="your token goes here" fb = facebook.GraphAPI(access_token=token) limit = 1 offset = 0 data = {"q": "jacob", "type": "user", "fields": "id", "limit": limit, "offset": offset} req = fb.request('/search', args=data, method='GET') users = [] for item in req['data']: users.append(item["id"]) pag = req['paging'] while pag.get("next") is not None: offset += limit data["offset"] = offset req = fb.request('/search', args=data, method='GET') for item in req['data']: users.append(item["id"]) pag = req.get('paging') print users 
0
source

Source: https://habr.com/ru/post/1213631/


All Articles