Python, NLTK, and TextBlob for Analyzing Sentiment

From recent tweets using Twitter API

Pankaj Kumar
13 min readSep 6, 2021
Photo by Ivan Lapyrin on Unsplash

In this post, I am going to use Twitter API to search recent tweets satisfying query criteria. The query I am using returns recent 100 tweets from the President of the United States (POTUS) ‘s public account. We’ll see how we construct this query and how to use authentication parameters and endpoint URL to get recent tweets. We’ll analyze tweet text and use NLTK and TextBlob packages to analyze the sentiment store and find how the sentiment scores are distributed for these tweets. The example is based on the official Twitter code for connecting to the endpoint.

Step 1

Find bearer token for TwitterAPI

We need a bearer token for querying data from Twitter. You need to create an account on Twitter and follow the steps on the following link to get Twitter API access.

Once you got access you ‘ll be able to generate bearer token. I have stored it in a file called data/twitter_credentials.yaml and read it from there. You can adopt other approaches like reading from environment variables as suggested in Twitter gitub code.

--

--

Pankaj Kumar

MS Data Science SMU TX. Pursuing MSc Financial Engg. At WQU.Interest in Algos, Discovering Trends fm data. Methodical, conven/non-conven. Investigation of data.