Kalebu Jordan

Become a Pro Python Developer

How to perform sentiment analysis from twitter using Python

Hello, Guys,

In this tutorial, I will guide you on how to perform sentiment analysis on textual data fetched directly from Twitter about a particular matter using tweepy and textblob.

what is sentiment analysis?

Sentiment analysis is a process of analyzing emotion associated with textual data using natural language processing and machine learning techniques.

If you’re new …

If you’re new to sentiment analysis in python I would recommend you watch emotion detection from the text first before proceeding with this tutorial.

what are we going to build ..

We are going to build a python command-line tool/script for doing sentiment analysis on Twitter based on the topic specified.

How will it work ?

You will just enter a topic of interest to be researched in twitter and then the script will dive into Twitter, scrap related tweets, perform sentiment analysis on them and then print the analysis summary.

Project requirements

To follow through tutorial you need the following

  • Sign up for twitter to Developers to get API Key
  • Install tweepy Libray (Twitter functionalities)
  • Install TextBlob Library (Natural language processing)

Twitter Developer (API KEYS)

You can follow through this link Signup in order to signup for twitter Developer Account to get API Key.

Once you signup for a developer account and apply for Twitter API, It might take just a few hours to a few days to get approval.

After being approved Go to your app on the Keys and Tokens page and copy your api_key and API secret key in form as shown in the below picture.

Tweepy Installation

The easiest way to install the latest version from PyPI is by using pip:

$ pip install tweepy

You can also use Git to clone the repository from GitHub to install the latest development version:

$ git clone https://github.com/tweepy/tweepy.git
$ cd tweepy
$ pip install .

TextBlob Installation

$ pip install -U textblob
$ python -m textblob.download_corpora

Building our Twitter analysis tool

Now after everything is clearly installed, let’s get hand dirty by coding our tool from scratch.

Put API_KEYS on separate file

First of all I have separated project into two files , one consisting api keys while others consisting our code for script .

├── API_KEYS.py
└── app.py

0 directories, 2 files

If we look inside the API_KEYS.py it look as shown below whereby the value of api_key and api_secret_key will be replaced by your credentials received from twitter


api_key = 'your api key'
api_secret_key = 'your api secret key'

Now Let’s start coding our script

Importing Libraries and dependencies
from tweepy import API,  OAuthHandler
from textblob import TextBlob
from API_KEYS import api_key, api_secret_key
Authenticating our script

To start fetching tweets from twitter, firstly we have to authenticate our app using api key and secret key.

To authenticate our api we will use OAuthHandler as shown below

authentication = OAuthHandler(api_key, api_secret_key)
api = API(authentication)
Fetching tweets on Particular Topic

To fetch tweets from twitter using our Authenticated api use search method fetch tweets about a particular matter .

#________Syntax __________________
public_tweets = api.search(Topic)

public_tweets is an iterable of tweets objects but in order to perform sentiment analysis we only require the tweet text.

Therefore in order to access text on each tweet we have to use text property on tweet object as shown in the example below.

from tweepy import API,  OAuthHandler
from textblob import TextBlob
from API_KEYS import api_key, api_secre

authentication = OAuthHandler(api_key, api_secret_key)
api = API(authentication)

corona_tweets = api.search('corona virus')
for tweet in corona_tweets:
  text = tweet.text

When you run the above script it will produce the result similar to what shown below .

RT @amyklobuchar: So on Frontier Airlines you now have to pay an extra fee to keep yourself safe from corona virus. As I said today at the…
There are so many disturbing news these days ON TOP OF CORONA VIRUS. It just sinks my heart😟 We all need therapy.
RT @ug_chelsea: Corona virus symptoms basically are the same feelings you get when your wife is checking your phone
Performing Sentiment analysis

Now Let’s use use TextBlob to perform sentiment analysis on those tweets to check out if they are positive or negative

Textblob Syntax to checking positivity or negativity

polarity = TextBlob(Text).sentiment.polarity
If polarity is less than 0 it's negative
If polarity is greater than 0 it's positive

Final Application

I then compiled the above knowledge we just learned to building the below script with addition of clean_tweets function to remove hashtags in tweets


from tweepy import API,  OAuthHandler
from textblob import TextBlob
from API_KEYS import api_key, api_secret_key

def clean_tweets(tweet):
    tweet_words = str(tweet).split(' ')
    clean_words = [word for word in tweet_words if not word.startswith('#')]
    return ' '.join(clean_words)

def analyze(Topic):
    positive_tweets, negative_tweets = [], []
    authentication = OAuthHandler(api_key, api_secret_key)
    api = API(authentication)
    public_tweets = api.search(Topic, count=10)
    cleaned_tweets = [clean_tweets(tweet.text) for tweet in public_tweets]
    for tweet in cleaned_tweets:
        tweet_polarity = TextBlob(tweet).sentiment.polarity
        if tweet_polarity<0:
    return positive_tweets, negative_tweets

positive, negative = analyze('Magufuli')
print(positive , '\n\n', negative)
print(len(positive), ' VS  ', len(negative))

To change a Topic you want to analyze or change Topic parameter in in analyze function to Topic you want.

Also you can specify the number of tweets to be fetched from twitter by changing the count parameter .

When you run the above application it will produce results to what shown below

 ['@o_abuga Obvious, the test kits the results are doubtful!! Magufuli said it']
9  VS   1

======================The end ==================================

Hope you find it interesting, now don’t forget to subscribe to this blog to stay updated on upcoming python tutorial.

I also recommend you to read this;

To get he full code for this article check it out on My Github

Leave a Reply


Enjoy this blog? Please spread the word :)