Scraping comments of a facebook post

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

6 thoughts on “Scraping comments of a facebook post”

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

    how to solve this ?

Let's discuss now...

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