Uts*_*v T 7 python pagination facebook
我的问题涉及学习如何使用Facebook的Python API检索我的整个朋友列表.当前结果返回一个具有有限数量的朋友的对象和一个指向"下一页"的链接.我如何使用它来获取下一组朋友?(请将链接发布到可能的重复项)任何帮助将不胜感激.一般来说,我需要了解API使用的分页.
import facebook
import json
ACCESS_TOKEN = "my_token"
g = facebook.GraphAPI(ACCESS_TOKEN)
print json.dumps(g.get_connections("me","friends"),indent=1)
Run Code Online (Sandbox Code Playgroud)
run*_*run 19
遗憾的是,近两年来,分页记录是一个悬而未决的问题.你应该能够使用请求这样分页(基于这个例子):
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9906 次 |
| 最近记录: |