• Offers
    • RegisterLogin
      • Learn More
    PythonPoint.netPythonPoint.net
    • Offers
    • RegisterLogin
      • Learn More

      Python

      SKILL IS IMPORTANT THAN DEGREE Be skill full.
      • Home
      • Blog
      • Python
      • How to extract data from Facebook using Python

      How to extract data from Facebook using Python

      • Posted by Python Point Team
      • Categories Python
      • Date December 31, 2022
      • Comments 0 comment
      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:

      1. A node is an individual object with a unique ID
      2. An edge is a connection between one node and another
      3. 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

      Image for post

      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:

      Image for post

      Select ‘Graph API Explorer’ here. This will open a new page:

      Image for post

      Here, in place of {event-id}, put the id of the event, like this:

      Image for post

      Hit submit. Also, on the same page, you will find below ‘get code’ option

      Image for post

      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

      • Share:
      author avatar
      Python Point Team

      Previous post

      How to export DataFrame to CSV in Python
      December 31, 2022

      Next post

      How to extract text from image in python?
      December 31, 2022

      You may also like

      15 Powerful Step for Mastering JSON Parsing in Python: Boosting Data Manipulation and Validation
      21 June, 2023

      Introduction In the world of programming, data plays a crucial role, and managing it efficiently is of utmost importance. JSON (JavaScript Object Notation) has emerged as a popular data interchange format due to its simplicity and flexibility. In this article, …

      Introduction to Transfer Learning with Python: A Practical Guide
      31 December, 2022

      Introduction: Definition of transfer learning Overview of how transfer learning works in the context of machine learning Why transfer learning is useful and important Section 1: Transfer learning in Python with Keras In this section, we will explore how to …

      How to Check Type in Python
      31 December, 2022

      In this article, we will learn to check type in Python. The built-in function type() can be used to check the type of data in Python.

      Subscribe
      Login
      Notify of
      Please login to comment
      0 Discussion
      Inline Feedbacks
      View all comments

      Latest Courses

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      ₹10,000
      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      ₹25,000 ₹2,500

      Latest Posts

      • 15 Powerful Step for Mastering JSON Parsing in Python: Boosting Data Manipulation and Validation
      • Introduction to Transfer Learning with Python: A Practical Guide
      • How to Check Type in Python
      • How to make web crawler in python?
      • Why was the language called “python”?
      Contact
      •   support@pythonpoint.com

      We get you the best Python Courses and Blogs aiming to provide skill.

      We Believe Skill is much more important than a Degree

      Company
      • About Us
      • Blog
      • Offers
      • Contact
      Useful Links
      • Courses
      Support
      • Need Support

      © 2020 ALL RIGHTS RESERVED​ PYTHONPOINT.NET

      PythonPoint

      • Terms of Use
      • Refund Policy
      • Privacy Policy

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now

      wpDiscuz