Basic introduction to tweey library – Twitter analysis using Python

Twitter is an online social networking site where users can post their messages and interact with their friends.
In this tutorial, we will learn how to perform basic task i.e. fetching friend’s list from twitter network, retrieving tweets, status. First step in this direction is to create a twitter app which will provide key and token. This tutorial assumes that you have obtained your key and token information already. In this tutorial, we will use tweepy python library which is wrapper for twiter REST api.

Installation of tweepy library

You can install tweepy library with following command

pip install tweepy


Establishing connection to twitter

import tweepy
from tweepy import OAuthHandler
consumer_key='your consumer key'
consumer_secret='your consumer secret'
access_token='your access tokens'
access_secret='your access token secret'

# Creating oauth handler isntance

# setting value for access token

# fetch tweepy api (wrapper for twitter REST api)

Fetching User’s timeline status

# access all status from timeline
for status in tweepy.Cursor(api.home_timeline).items():
    print status.text
    # accessing text using json response
    print status._json['text']

Fetching Friend’s list

for friend in tweepy.Cursor(api.friends).items(): 
    print friend._json['name']

Fetching User’s tweets

for status in tweepy.Cursor(api.user_timeline).items(): 
    print status._json['text']

Updating status

For updating status using tweepy call you need to have write permission for your twitter app.

api.update_status(status='This tweet made by tweepy')

Monitoring live tweet updates

This is the most widely used method in research to track live tweets for further analysis. Tweepy library provides methods to monitor tweets and analyze. Python script given below will keep connection open and keep on storing tweets in a file.

from tweepy import Stream
from tweepy.streaming import StreamListener

#extending Streamlistener class
class MyListener(StreamListener):
    def on_data(self,data):
            # Opening a file to store tweets
            with open('tweet.json','a') as f:
                return True
        except BaseException as e:
            print 'Error on data'
        return True
    def on_error(self,status):
        print status
        return True
# Creating streamlistener

# Filtering tweets which contain terrorist hashtag

Post analysis of stored tweets

For post processing of stored tweets (json format) we will use json python library.

import json
with open('tweet.json','r') as f:
    for line in lines: