Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Last active April 12, 2023 17:45
Show Gist options
  • Save ryantuck/64896882fcca67903f1a16815b4e33c8 to your computer and use it in GitHub Desktop.
Save ryantuck/64896882fcca67903f1a16815b4e33c8 to your computer and use it in GitHub Desktop.
Scraping Facebook data using its Graph API

python sdk

Install facebook SDK

pip3 install facebook-sdk

Pull data:

import facebook

app_id = 'XXX'
app_secret = 'XXX'
access_token = '{}|{}'.format(app_id, app_secret)

graph = facebook.GraphAPI(access_token, version='2.4')

posts

Get all public NYTimes posts:

posts = graph.get_object('nytimes', fields='posts')

A specific post will look like:

{'created_time': '2017-10-30T01:25:01+0000',
 'id': '5281959998_10151358859034999',
 'message': 'From our earliest moments, mindfulness can help minimize anxiety and increase happiness.'}

comments

Get comments for this post:

comments = graph.get_object('5281959998_10151358859034999', fields='comments')

A comment will look like:

{'created_time': '2017-10-30T01:40:29+0000',
 'from': {'id': '535109076827698', 'name': 'Daniel Polowetzky'},
 'id': '10151358859034999_10151362281014999',
 'message': 'How does mindfulness differ from concentration?Mindfulness practitioners are always instructing you to focus on your breathing. Is concentrating on a mathematicalproof being in the present? After all, it’s not thinking about the past or future. I suspect that is not what they’re talking about.'}

reactions

Get reactions:

reactions = graph.get_object('5281959998_10151358859034999', fields='reactions')

Reactions look like:

   {'id': '10101405282320308', 'name': 'Ariana J. Giraldo', 'type': 'SAD'},
   {'id': '10156742255443032', 'name': 'Athina Aston', 'type': 'WOW'},
   {'id': '10214361424607900', 'name': 'Seema Kochery', 'type': 'LIKE'},
   {'id': '10105657659876174', 'name': 'Maggie Mae', 'type': 'HAHA'},
@apoorvthedude
Copy link

Code need to work if we have a business-type app & have the app review otherwise it will through error like :
facebook.GraphAPIError: (#100) Object does not exist, cannot be loaded due to missing permission or reviewable feature, or does not support this operation. This endpoint requires the 'pages_read_engagement' permission or the 'Page Public Content Access' feature or the 'Page Public Metadata Access' feature. Refer to https://developers.facebook.com/docs/apps/review/login-permissions#manage-pages, https://developers.facebook.com/docs/apps/review/feature#reference-PAGES_ACCESS and https://developers.facebook.com/docs/apps/review/feature#page-public-metadata-access for details.

Does we need permission & have to review our app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment