Sentiment Analysis using Twitter API and Python

Jason
3 min readOct 21, 2020

--

In this article, I will show you how to use Twitter API v1.1 to retrieve and do basic sentiment analysis of tweets from a user. More specifically, I will analyze the tweets of the Elon Musk.

This article assumes that you have basic knowledge of Python and REST APIs. If not, you can look at my other articles to learn more.

Importing Python Libraries

First, we will be importing Python libraries needed for the application. If you do not have these libraries installed, you can do a pip install for each library.

#Importing Python Libraries
import json
import requests
import re
from textblob import TextBlob
import matplotlib.pyplot as plt

Using Twitter API Tokens

You should receive an API key, API secret, and Bearer Token when creating a Twitter project at https://developer.twitter.com/. We will store the Bearer Token as a variable. Replace the string below with the Bearer Token you have received from Twitter.

bearerToken = "Bearer Token from Twitter"

Making the REST call to Twitter

We will now be setting up our application to make the API REST call to Twitter to retrieve tweets from Elon Musk.

Setting Up the API Request

In order to retrieve tweets from Twitter, we will need to make a REST call to an endpoint that Twitter exposes. We will save the endpoint as a variable.

endpoint = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=elonmusk&include_rts=false&count=1000"

Authenticating the API Request

In order to make a REST call, we will need to authenticate the request using the Bearer Token we saved earlier. We will need to pass the token as a header in order for the request to succeed.

headers = {"Authorization": 'Bearer ' + bearerToken}

Making the REST call

We are now ready to make the REST call to Twitter. We will be making a GET request to retrieve the tweets from Elon Musk. We will convert to request to JSON format and store the data in JSON format into a variable called data.

request = requests.get(endpoint, headers=headers)
data = request.json()

Parsing the JSON Data

We will now parse the data in order to get the text from each tweet and inserting each tweet to a list.

tweets = []
for element in data:
tweets.append(element['text'])

Next, we will convert each tweet to a TextBlob object, which will analyze the text, and give a value for the sentiment as field in the object.

sentiment_objects = [TextBlob(tweet) for tweet in tweets]

Then, we will store all the sentiment values into another list.

sentiment_values = [tweet.sentiment.polarity for tweet in sentiment_objects]

Plotting the Sentiment Values

We will now plot all the values that we have retrieved into a graph.

plt.plot(sentiment_values)
plt.show()

We can see the ranges of Elon Musk’s sentiment value for his most recent tweets.

  • A sentiment value closer to 1 indicates a more positive tweet
  • A sentiment value closer to 0 indicates a more neutral tweet
  • A sentiment value closer to -1 indicates a more negative tweet

We can see that Elon Musk’s recent tweets are more positive.

Conclusion

This concludes my article on basic sentiment analysis of tweets using Python and the Twitter API. If you would like to learn more about software, I will be posting more topics related to software and technology!

Please reach out if you are having issues with the code posted above and I will get back to you. If you have any requests for topics to write about, I will gladly share my knowledge.

Thank you for reading my article!

--

--