Python Facebook API - cursor pagination

后端 未结 3 1456
傲寒
傲寒 2021-02-09 08:10

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

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 08:55

    in this example you off set / pagination by one at the time, i think my while loop is simple since it only looking for the pagination key"next" to be none, if doesnt exists means we finish looping, and you will have your results in a list. in this example i am just looking for all the people call 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
    

提交回复
热议问题