如何使用python代码和facebook图形api获取我所有的facebook帖子。我试过使用这个代码:
import json
import facebook
def get_basic_info(token):
graph = facebook.GraphAPI(token)
profile = graph.get_object('me',fields='first_name,last_name,location,link,email')
print(json.dumps(profile, indent=5))
def get_all_posts(token):
graph = facebook.GraphAPI(token)
events = graph.request('type=event&limit=10000')
print(events)
def main():
token = "my_token"
#get_basic_info(token)
get_all_posts(token)
if __name__ == '__main__':
main()
我收到一个错误,上面写着,"GraphAPIError:(#33(此对象不存在或不支持此操作"。
似乎所有其他stackoverflow问题都非常古老,不适用于最新版本的facebook图形API。我不完全确定你是否可以使用facebook图形api来完成这项工作。如果使用这种技术无法做到这一点,有没有其他方法可以使用python获得我的帖子?请注意,函数get_basic_info((运行良好。
我假设您想要获取用户事件:https://developers.facebook.com/docs/graph-api/reference/user/events/
注意:
此边缘仅适用于数量有限的已批准应用程序。查询此边缘的未经批准的应用程序将收到一个空数据集作为响应。此时无法请求访问此边缘。
无论哪种方式,API都不是type=event&limit=10000
,而是/me/events
。
我在@luschn的第一个答案的帮助下解决了这个问题我又犯了一个错误,那就是使用事件来获取我所有的posts。而我本应该在代码中使用me/posts
。以下是在版本6中完美工作的函数。
def get_all_posts(graph):
posts = graph.request('/me/posts')
count=1
while "paging" in posts:
print("length of the dictionary",len(posts))
print("length of the data part",len(posts['data']))
for post in posts["data"]:
print(count,"n")
if "message" in post: #because some posts may not have a caption
print(post["message"])
print("time : ",post["created_time"])
print("id :",post["id"],"nn")
count=count+1
posts=requests.get(posts["paging"]["next"]).json()
print("end of posts")
这里,post["data"]
只提供前25个帖子,所以我使用posts["paging"]["next"]
链接来获取下一页,只要有下一页。