In this tutorial, we will build a program that can analyze the popularity and sentiment of hashtags on Twitter.
To accomplish this we'll use Python, the Tweepy library to interact with the Twitter API for fetching tweets, and the TextBlob library for sentiment analysis.
Note: This program will run from Terminal (Linux) or Command Prompt (Windows). If you don't have python installed go to https://www.python.org/downloads/ download and install the appropriate version for your system (should be selected automatically when you enter).
So, let's begin:
Step 1: Set Up Twitter API Access
To access the Twitter API, you'll need to create a Twitter Developer account and generate API keys. Follow these steps:
- Go to https://developer.twitter.com/ and sign in with your Twitter account.
- Apply for a developer account if you don't have one already.
- Once your developer account is approved, create a new app and generate the API keys (consumer key, consumer secret, access token, and access token secret).
Step 2: Install Dependencies
Before we begin coding, make sure you have the necessary dependencies installed. Open your terminal or command prompt and run the following commands:
pip install tweepy
pip install textblob
Step 3: Import Libraries and Set Up Authentication
Now, let's import the required libraries and set up authentication with the Twitter API. Create a new Python file (e.g., hashtag_analyzer.py
) and add the following code:
import tweepy
from textblob import TextBlob
# Twitter API credentials
consumer_key = 'YOUR_CONSUMER_KEY'
consumer_secret = 'YOUR_CONSUMER_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
Make sure to replace 'YOUR_CONSUMER_KEY'
, 'YOUR_CONSUMER_SECRET'
, 'YOUR_ACCESS_TOKEN'
, and 'YOUR_ACCESS_TOKEN_SECRET'
with your actual API keys.
Step 4: Define the Hashtag Analyzer Function
Next, let's define a function that takes a hashtag as input, fetches tweets containing that hashtag, and performs sentiment analysis on the retrieved tweets. Add the following code to your Python file:
def analyze_hashtag(hashtag):
tweets = tweepy.Cursor(api.search, q=hashtag, tweet_mode='extended').items(100)
# Variables to keep track of sentiment analysis results
total_polarity = 0
total_subjectivity = 0
tweet_count = 0
# Perform sentiment analysis on each tweet
for tweet in tweets:
# Ignore retweets
if 'retweeted_status' in tweet._json:
continue
# Perform sentiment analysis using TextBlob
blob = TextBlob(tweet.full_text)
polarity = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity
# Update sentiment analysis results
total_polarity += polarity
total_subjectivity += subjectivity
tweet_count += 1
# Calculate average sentiment scores
average_polarity = total_polarity / tweet_count
average_subjectivity = total_subjectivity / tweet_count
# Print the results
print(f"Hashtag: {hashtag}")
print(f"Number of tweets analyzed: {tweet_count}")
print(f"Average polarity: {average_polarity}")
print(f"Average subjectivity: {average_subjectivity}")
Step 5: Testing the Hashtag Analyzer function
You can now test the hashtag analyzer by calling the analyze_hashtag()
function with a hashtag of your choice. Add the following code to your Python file:
if __name__ == "__main__":
hashtag = input("Enter a hashtag to analyze: ")
analyze_hashtag(hashtag)
TLDR
Here's how the full code should look like:
import tweepy
from textblob import TextBlob
consumer_key = 'YOUR_CONSUMER_KEY'
consumer_secret = 'YOUR_CONSUMER_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def analyze_hashtag(hashtag):
tweets = tweepy.Cursor(api.search, q=hashtag, tweet_mode='extended').items(100)
total_polarity = 0
total_subjectivity = 0
tweet_count = 0
for tweet in tweets:
if 'retweeted_status' in tweet._json:
continue
blob = TextBlob(tweet.full_text)
polarity = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity
total_polarity += polarity
total_subjectivity += subjectivity
tweet_count += 1
average_polarity = total_polarity / tweet_count
average_subjectivity = total_subjectivity / tweet_count
print(f"Hashtag: {hashtag}")
print(f"Number of tweets analyzed: {tweet_count}")
print(f"Average polarity: {average_polarity}")
print(f"Average subjectivity: {average_subjectivity}")
if __name__ == "__main__":
hashtag = input("Enter a hashtag to analyze: ")
analyze_hashtag(hashtag)
Again, remember to replace 'YOUR_CONSUMER_KEY'
, 'YOUR_CONSUMER_SECRET'
, 'YOUR_ACCESS_TOKEN'
, and 'YOUR_ACCESS_TOKEN_SECRET'
with your actual API keys.
Save the file and run it from the terminal or command prompt using the command python hashtag_analyzer.py
. Enter a hashtag when prompted, and the program will fetch tweets, perform sentiment analysis, and display the results.
Note: Keep in mind that Twitter API has rate limits, so you might encounter restrictions on the number of tweets you can fetch within a specific timeframe.
Top comments (0)