Python HOW: Getting Facebook Data and Insights Using facebook-sdk

Image for post
Image for post
Photo by Pixabay from Pexels

At the end of this article you will learn how to get valuable data and insights of a page you’re an admin. It assumes you’ve already obtained a permanent page token. If you’ve not, check my 👉 article first

The Graph API 🍇

Check the latest version of the official facebook-sdk release

Page Data 🎯

to learn how to get the page ID check 👉 article
Have a look at all the available fields here. The id field is always returned

Page Insights 🔍

We can preset the date range (start from), specify the aggregation period (starting from the preset date), and show the metric description

Latest Posts IDs

Note how we specified some fields as a method of posts itself

All/Specific Posts IDs

The default value of until is now (i.e. most recent post)
63 posts were created this year (since ‘2019–01–01T09:36:00+0000’ until ‘2019–02–12T12:48:13+0000’)

Post data

Note how we called the summary method on comments and reactions
Check all the available parameters here

Post Insights

Note that we used the post ID not the page ID

Video Post Insights

Note that we used the object ID (i.e the video) not the post ID

TL;DR: full code

Written by

I’m an End-to-End data scientist and a Python educator. Most of my articles start after saying “I wish someone has written about this!”, maybe I should?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store