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 developer.facebook.com. Open Graph API Explorer in developers.facebook.com/tools/explorer and click on “Get Token” button.

Mining Facebook Public Data – Posts

Now we will use Python to get Facebook Posts data and export it in CSV file.

import requests 
import pandas as pd
import os, sys

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

fb_pageid = "100281786832302"
postlst = []

url = "https://graph.facebook.com/v2.9/"+fb_pageid+"/posts?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(postlst), url)    

print "\nGenerating CSV File"

df = pd.DataFrame({'dates': postlst})
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 


This CSV file can be easily imported in excel to get descriptive statistics.



One Reply to “Facebook data mining using Python”

  1. Hola everything is going sound here and ofcourse every one is sharing data, that’s really fine, keep up writing. grazie

Leave a Reply

Your email address will not be published. Required fields are marked *