Trying to extract a month worth of posts using Cursor Based Pagination on Facebook Graph API but failed
I'm trying to extract a month worth of facebook posts into a csv file. I'm extracting from the 1st May until 30th May of 2024, but once my script done running, it only extracted posts from the 30th May 2024 (it started on this date) until 12th May 2024.
It doesn't extract from the 9th May until 1st May 2024.
Below is my code:
import pyfacebook from datetime import datetime, timedelta, timezone import json import pandas as pd import time # Initialize Facebook Graph API connection (replace placeholders with your actual credentials) graph = pyfacebook.GraphAPI(access_token='my-access-token', version='v20.0') # Define the page ID page_id = 'my-page-id' # date from_date = '2024-05-01' to_date = '2024-05-30' # Construct the API request URL (notice the parameters 'since', 'until' and 'fields') posts_url = f'/{page_id}/feed?fields=attachments,created_time&since={from_date}T08:00:00&until={to_date}T23:59:59' posts_data = graph._request(posts_url).json() while True: if 'paging' in posts_data and 'next' in posts_data['paging']: posts_url = posts_data['paging']['next'] posts_data = graph._request(posts_url).json() time.sleep(2) # Increased delay to be safe else: break # No more pages to retrieve df = pd.DataFrame(posts_data) df.to_csv('facebook_data.csv', index=False)
Here is the result, it ends on the 9th May 2024 instead of continuing until 1st May 2024:
How do I extract the full month's worth of data?