Sentiment Analysis of Facebook Comments with Python

In this post, we will learn how to do Sentiment Analysis on Facebook comments. We will use Facebook Graph API to download Post comments. Finally, we run a python script to generate analysis with Google Cloud Natural Language API.

Download Facebook Comments

import requests 
import requests 
import pandas as pd
import os, sys

token = ""
    token = os.environ['FB_TOKEN']
    print "Set FB_TOKEN variable"

fb_pageid = "228735667216"
fb_postid = "10154922624762217"
commentlst = []
datelst = []

url = ""+ fb_pageid +"_"+ fb_postid +"/comments?limit=100&access_token="+token

    posts = requests.get(url)
    posts_json = posts.json()
    for x1 in posts_json['data']:
    next_page = ""
        next_page = posts_json['paging']['next']
        url = next_page
    if not next_page: break
    print "Count: %s,  Next Page: %s" % ( len(commentlst), url)

print "\nGenerating JSON File"

df = pd.DataFrame({'comment': commentlst, 'dates': datelst})
df['dates'] = pd.to_datetime(df['dates'])
df['day_of_week'] = df['dates'].dt.weekday_name
df['year'] = df['dates'].dt.year
df['month'] = df['dates'].dt.month
df['count'] = 1 


Generate Sentimental Results

import requests 
import json
from import language, exceptions

client = language.Client()
# export GOOGLE_APPLICATION_CREDENTIALS environment variable 

with open('comment_data.json') as data_file:
    data = json.load(data_file)

sentiment_list = []
for x1,y1  in data['comment'].items():
        document = client.document_from_text(y1)
        sentiment = document.analyze_sentiment().sentiment
        sentiment_list.append({"id": x1, "comment": y1, "sentiment_score": sentiment.score, "sentiment_magnitude": sentiment.magnitude })
        print "Pass"
        print "Fail"

with open('sentiment_comments.json', 'w') as outfile:
    json.dump(sentiment_list, outfile)


Based on our sentiment analysis of BBC Facebook post, we have below matrix:
Positive Score: 33%
Negative Score 48%
Neutral_score 19%

Facebook data mining using Python

Facebook data will help you to increased exposure to your potential customers. We use Python to get publicly available data from Facebook which will be later combined with website traffic and other data sources.

Generate Facebook Token
To extract data from Facebook we need to generate a token from Open Graph API Explorer in and click on “Get Token” button.
Continue reading “Facebook data mining using Python”