Scraping comments of a facebook post

You need to submit your app for review with required permission to access Facebook posts of page in which you aren’t admin; in live mode

This is a basic example of scrapping comments of a facebook post using python and facebook sdk

I hope you have python and know how to install a package.

Lets install facebook using pip

pip install facebook-sdk

We are proceeding directly in python

Before that we need an access token to connect to facebook.

Get the access token from here: https://developers.facebook.com/tools/accesstoken/

Access token is the pretty long string inside the textbox on the side where it is written “Access Token”

Now lets get those comments

graph = facebook.GraphAPI(access_token=’your pretty long string which you have copied now’)

post = graph.get_object(id = ‘post id’)

You can get the post id, by opening an url of a post in facebook

If url to a post is https://www.facebook.com/TimesofIndia/posts/10153947250687139 then, “10153947250687139” is the post id

print post

This will print bunch of details about the post including first 10 likes and first 10 comments

If you just need to see the comments

print post[‘comments’]

This will show just the comments.

If you need the remaning comments/likes, you will need to iterate the “cursors”, which doesn’t cover in basics. So just stopping here.

If you need further clarifications, please comment. If you wish to have Part 2 of this post, comment.

Njoy smile


Comments

6 responses to “Scraping comments of a facebook post”

  1. how to get all comments..

    1. will update the article using requests where cursor iteration will be specified

  2. how to get comment details of a post

  3. how can i get comments with their respective replies?

  4. Praveen kumar Polimeni Avatar
    Praveen kumar Polimeni

    using the above syntax for getting the comments on a post, it is failing giving a key error : comments

    how to solve this ?

    1. Probably there is a change. I will publish an updated one.

Leave a Reply to beginnerdotcomCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.