How to extract data from Facebook using Python
In this article, we will learn to extract data from Facebook using Python.
Data on Facebook is represented using the ‘graph’. To interact with a graph we use an HTTPS-based API called the Graph API.
Node, edge and fields are the hierarchical components of a graph:
- A node is an individual object with a unique ID
- An edge is a connection between one node and another
- Fields are the node properties
The first thing to be able to extract data from Facebook using python is to register as a developer on Facebook and then have an access token. Go to link developers.facebook.com, create an account. The next step is to go to the link developers.facebook.com/tools/explorer and in the ‘My apps’ drop-down select ‘add a new app’. Again go back to the same link developers.facebook.com/tools/explorer. You will see ‘Graph API Explorer’ in the ‘My Apps’ dropdown. From ‘Graph API Explorer’ drop down, select your app.
Then, select ‘Get Token’. From this drop down, select “Get User Access Token” and then select “Get Access Token.”
Python Code to Access Facebook Public Data:
First of all, the required libraries are urllib3
, facebook
and request
.
In this article, we are going to get data on events associated with the topic Football and we are limiting the events’ number to 10000:
graph = facebook.GraphAPI(access_token=token, version = 2.7) events = graph.request(‘/search?q=Football&type=event&limit=10000’)
This will make a dictionary of all the events that have been created on Facebook and has the string ‘Football’ in its name. To get the list of events:
eventList = events['data']
The next step is extracting all information for an event from the list of events extracted above. For that, we need the event id. Get the EventID of the first event in the list by
eventid = eventList[1]['id']
For this EventID, get all information and set few variables which will be used later by:
event1 = graph.get_object(id=eventid, fields=’attending_count,can_guests_invite,category,cover,declined_count,description,end_time,guest_list_enabled,interested_count,is_canceled,is_page_owned,is_viewer_admin,maybe_count,noreply_count,owner,parent_group,place,ticket_uri,timezone,type,updated_time’) attenderscount = event1['attending_count'] declinerscount = event1['declined_count'] interestedcount = event1['interested_count'] maybecount = event1['maybe_count'] noreplycount = event1['noreply_count']
Getting the list of all those who are attending an event and converting the response into JSON format:
attenders = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/attending?access_token="+token+”&limit=”+str(attenderscount)) attenders_json = attenders.json()
Getting the admins of the event:
admins = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/admins?access_token="+token)admins_json = admins.json()
And similarly you can extract other information such as photos/videos/feed of that event if you want.
Go to https://developers.facebook.com/docs/graph-api/reference/event/ and see ‘Edges’ part in the documentation. See image
Now, let’s say, you want to have a list of all those who are interested in the event, click on ‘interested’ green word here. This will open up a new page:
Select ‘Graph API Explorer’ here. This will open a new page:
Here, in place of {event-id}, put the id of the event, like this:
Hit submit. Also, on the same page, you will find below ‘get code’ option
Select it to see the code. Select ‘curl’ in the pop up that appears and then get the same output in the python code, write it with requests.get as it has been shown in above examples.
References: https://towardsdatascience.com/how-to-use-facebook-graph-api-and-extract-data-using-python-1839e19d6999