Last active
May 10, 2018 17:32
-
-
Save my-slab/10294040 to your computer and use it in GitHub Desktop.
Basic example of using the Python for Facebook SDK to get all the posts on a user's timeline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import facebook | |
import requests | |
def some_action(post): | |
""" Here you might want to do something with each post. E.g. grab the | |
post's message (post['message']) or the post's picture (post['picture']). | |
In this implementation we just print the post's created time. | |
""" | |
print post['created_time'] | |
# You'll need an access token here to do anything. You can get a temporary one | |
# here: https://developers.facebook.com/tools/explorer/ | |
access_token = '' | |
# Look at Bill Gates's profile for this example by using his Facebook id. | |
user = 'BillGates' | |
graph = facebook.GraphAPI(access_token) | |
profile = graph.get_object(user) | |
posts = graph.get_connections(profile['id'], 'posts') | |
# Wrap this block in a while loop so we can keep paginating requests until | |
# finished. | |
while True: | |
try: | |
# Perform some action on each post in the collection we receive from | |
# Facebook. | |
[some_action(post=post) for post in posts['data']] | |
# Attempt to make a request to the next page of data, if it exists. | |
posts = requests.get(posts['paging']['next']).json() | |
except KeyError: | |
# When there are no more pages (['paging']['next']), break from the | |
# loop and end the script. | |
break |
This program gives me all the created_times, which is ok. But when I try with print(post['message']) it gives me only the last 5 posts (the 5th one is on 25 july). Do you have any idea why?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HI Martey
This code stops when it gets an image in the post which doesn't have text. So im unable to retrieve all the post. Could you modify the code so that i can extract everything out of the fb page.