In [ ]:
# 輸入 token
token = input()
In [ ]:
import facebook
import requests
import json
In [ ]:
graph = facebook.GraphAPI(token, version="2.7")
In [ ]:
graph.get_object('me')
In [ ]:
# 讀取自己的貼文
# 方法 1
graph.get_connections('me', 'posts')
In [ ]:
# 方法 2
# 一次讀一百篇
graph.get_object('me', fields=json.dumps(['posts.limit(100)']))
In [ ]:
# 方法 3
posts = graph.get_all_connections('me', 'posts')
for i in range(50):
p = posts.next()
print(p)
方法4 自己刻
設定一個工具函數,讀取所有分頁 data
In [ ]:
def get_all_data(graph, req, N=0):
r = graph.get_object(req)
rtn = []
while N==0 or len(rtn)<N:
rtn.extend(r['data'])
if 'next' in r.get('paging', []):
r=requests.get(r['paging']['next']).json()
else:
break
return rtn
讀取自己的貼文,超過 100 篇就停止。
In [ ]:
posts = get_all_data(graph, 'me/posts', 100)
print (len(posts))
In [ ]:
posts
In [ ]:
# 參考
#%load q_posts_friends.py