user1406716
user1406716

Reputation: 9705

Unable to get Facebook Group members after first page using Python

I am trying to get the names of members of a group I am a member of. I am able to get the names in the first page but not sure how to go to the next page:

My Code:

url = 'https://graph.facebook.com/v2.5/1671554786408615/members?access_token=<MY_CUSTOM_ACCESS_CODE_HERE>'
json_obj = urllib2.urlopen(url)
data = json.load(json_obj)
for each in data['data']:
    print each['name']

Using the code above I am successfully getting all names on the first page but question is -- how do I go to the next page?

In the Graph API Explorer Output screen I see this:

enter image description here

What change does my code need to keep going to next pages and get names of ALL members of the group?

Upvotes: 0

Views: 574

Answers (1)

Jorge Torres
Jorge Torres

Reputation: 1465

The JSON returned by the Graph API is telling you where to get the next page of data, in data['paging']['next']. You could give something like this a try:

def printNames():
    json_obj = urllib2.urlopen(url)
    data = json.load(json_obj)
    for each in data['data']:
        print each['name']
    return data['paging']['next']  # Return the URL to the next page of data


url = 'https://graph.facebook.com/v2.5/1671554786408615/members?access_token=<MY_CUSTOM_ACCESS_CODE_HERE>'

url = printNames()
print "====END OF PAGE 1===="
url = printNames()
print "====END OF PAGE 2===="

You would need to add checks, for instance ['paging']['next'] will only be available in your JSON object if there is a next page, so you might want to modify your function to return a more complex structure to convey this information, but this should give you the idea.

Upvotes: 1

Related Questions