Twitter sentiment analysis in just 2 steps
We previously used the tweepy library to download tweets. Now we shall endeavour to, within 3 steps, download and sentiment score the…
Twitter sentiment analysis in just 2 steps
We previously used the tweepy library to download tweets. Now we shall endeavour to, within 3 steps, download and sentiment score the tweets.
Step 1 — Libraries and basic configuration
We first import the tweepy library, to help us make fetch Twitter data via its API, and the textblob library to help us perform sentiment scoring. And we set the keys required to access Twitter’s API, and authenticate.
import tweepy
from textblob import TextBlob
# Enter your own keys
API_KEY =''
API_SECRET =''
ACCESS_TOKEN =''
ACCESS_TOKEN_SECRET =''
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)Step 2 — Fetch and sentiment score tweets
Fetch the data, pass the text into a TextBlob, and print out the results.
for tweet in tweepy.Cursor(api.search,
q=topics[0],
count=100,
lang='en',
since='2018-08-10').items():
tweetblob = TextBlob(tweet.text)
print(tweet.created_at)
print(tweet.text)
print(tweetblob.sentiment.polarity)And that’s it. As easy peasy as easy peasy can be.
The Jupyter notebook with the code is here
playgrd.com || facebook.com/playgrdstar || instagram.com/playgrdstar/


