Book Image

Mastering Social Media Mining with Python

By : Marco Bonzanini
Book Image

Mastering Social Media Mining with Python

By: Marco Bonzanini

Overview of this book

Your social media is filled with a wealth of hidden data – unlock it with the power of Python. Transform your understanding of your clients and customers when you use Python to solve the problems of understanding consumer behavior and turning raw data into actionable customer insights. This book will help you acquire and analyze data from leading social media sites. It will show you how to employ scientific Python tools to mine popular social websites such as Facebook, Twitter, Quora, and more. Explore the Python libraries used for social media mining, and get the tips, tricks, and insider insight you need to make the most of them. Discover how to develop data mining tools that use a social media API, and how to create your own data analysis projects using Python for clear insight from your social data.
Table of Contents (10 chapters)

Mining your posts

After introducing the Python facebook-sdk with a simple example, we will start digging into the data mining opportunities. The first exercise is to download our own posts (that is, the posts published by the authenticated user).

The facebook_get_my_posts.py script connects to the Graph API and gets a list of posts published by the authenticated user me. The posts are saved in the my_posts.jsonl file using the JSON Lines format that we have already adopted in Chapter 2, #MiningTwitter - Hashtags, Topics, and Time Series, and Chapter 3, Users, Followers, and Communities on Twitter, (each line of the file is a JSON document):

# Chap04/facebook_get_my_posts.py 
import os 
import json 
import facebook 
import requests 
 
if __name__ == '__main__': 
  token = os.environ.get('FACEBOOK_TEMP_TOKEN') 
 
  graph = facebook.GraphAPI(token) 
  posts = graph.get_connections('me', 'posts') 
 
  while True:  # keep paginating 
    try: 
      with...