count number of tweets with hashtag

Count Tweets by Hashtag: A Step-by-Step Guide

By Michael Moore
AD

Monitoring the impact of social media campaigns often involves tracking the reach and engagement of specific hashtags. The number of tweets featuring a certain hashtag can provide valuable insights into the popularity and virality of a campaign. Here, we'll explore how to count the number of tweets with a specific hashtag using Twitter's API and some popular programming languages.

Before we delve into the technical aspects, it's important to understand that Twitter's API returns only a sample of tweets that match your search query. This is known as the 'Firehose' limitation. However, for most analytical purposes, this sample is sufficient.

Accessing Twitter's API

The first step in counting tweets with a hashtag is to access Twitter's API. You'll need to create a developer account and an app to get your API keys and access tokens.

AD
Fake tweets
Fake tweets
A tweet with a hashtag reveals in 'Twitter analysis report' that 'you can not get favorable response'
A tweet with a hashtag reveals in 'Twitter analysis report' that 'you can not get favorable response'
11 charts that show how you can use Twittercounter to check your impact on social media as a…
11 charts that show how you can use Twittercounter to check your impact on social media as a…

Twitter offers several methods to access its API, including REST API, Streaming API, and Search API. For counting tweets with a hashtag, the Search API is the most suitable, as it allows you to retrieve tweets containing specific keywords or hashtags within a specific time frame.

Twitter Developer Account

To create a Twitter Developer account, log in to your Twitter account, go to the 'Analytics' tab, and select 'Tools and apps'. Click on 'Apply for access' to create a new developer account.

Service "countingtweets.com" that displays the number of times the link in the page was tweeted on Twitter
Service "countingtweets.com" that displays the number of times the link in the page was tweeted on Twitter
Venngage Blog
Venngage Blog
Twitter Faceoff @GDTnews vs @Joey_C
Twitter Faceoff @GDTnews vs @Joey_C

After your application is approved, you'll be able to access the 'Apps' section where you can create a new app and obtain your API keys and access tokens.

Installing Tweepy Library

Tweepy is a popular Python library that simplifies interacting with Twitter's API. To install it, open your terminal or command prompt and type:

AD

pip install tweepy

two tweets are on the same page
two tweets are on the same page
Twitter Explains Why it's Removing Share Counts from Tweet Buttons
Twitter Explains Why it's Removing Share Counts from Tweet Buttons
Kyiah Ollieceon
Kyiah Ollieceon
Sizing Up Twitter Users
Sizing Up Twitter Users
Top 9 Hashtag Strategies: To Boost your Online Marketing Campaign - CEOWORLD magazine
Top 9 Hashtag Strategies: To Boost your Online Marketing Campaign - CEOWORLD magazine
the twitter page is shown with several tweets and other things to see on it
the twitter page is shown with several tweets and other things to see on it

Counting Tweets Using Python

Now that we have access to Twitter's API and the necessary Python library, let's write a simple script to count the number of tweets containing a specific hashtag. We'll use Tweepy's Cursor method to paginate through tweets and count them.

Here's a basic Python script illustrating this:

AD

Counting Tweets with Tweepy

First, import the necessary modules and set up your Twitter API credentials:

How Many Hashtags Should You Use on Instagram? (2025 Data)
How Many Hashtags Should You Use on Instagram? (2025 Data)
Counter Counting Close-up Youtube Video Views
Counter Counting Close-up Youtube Video Views
the tweet is being used to describe what people are doing on social media
the tweet is being used to describe what people are doing on social media
How to Use Twitter Hashtags as a Prospecting Tool : Social Media Examiner
How to Use Twitter Hashtags as a Prospecting Tool : Social Media Examiner
Let
Let
Hashtags and Social Media
Hashtags and Social Media
How Many Hashtags for Instagram in 2024: Only 5? | LouiseM
How Many Hashtags for Instagram in 2024: Only 5? | LouiseM
#️⃣ Use Hashtags the Right Way or Don’t Bother 🚀
#️⃣ Use Hashtags the Right Way or Don’t Bother 🚀
Hi dear hope you doing well . To make more money you need more followers For more followers and lik
Hi dear hope you doing well . To make more money you need more followers For more followers and lik
How To Get More Followers
How To Get More Followers
Hashtag Research Tools And Management - The Complete List #Hashtag
Hashtag Research Tools And Management - The Complete List #Hashtag
the best number of hashs on every social network
the best number of hashs on every social network
Oregon spying on Twitter users for Black Lives Matter hashtag
Oregon spying on Twitter users for Black Lives Matter hashtag

import tweepy 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)

Next, create a function to count tweets with a specific hashtag:

AD

def count_tweets_with_hashtag(hashtag): count = 0 for tweet in tweepy.Cursor(api.search, q=f'#{hashtag}', lang='en', tweet_mode='extended').items(100): count += 1 return count

Finally, call this function with the desired hashtag:

print(f'Number of tweets with #{count_tweets_with_hashtag(#example)}:')

Counting Tweets Using Other Languages

Besides Python, several other programming languages provide libraries to interact with Twitter's API. Here are brief examples of counting tweets with a hashtag in JavaScript and R:

Counting Tweets with Node-Twitter Library

In JavaScript, you can use the 'node-twitter' library. Here's a simple example:

const Twit = require('twit'); let T = new Twit({ consumer_key: 'your_consumer_key', consumer_secret: 'your_consumer_secret', access_token: 'your_access_token', access_token_secret: 'your_access_token_secret', }); async function countTweetsWithHashtag(hashtag) { let response = await T.get('search/tweets', { q: `#${hashtag}`, count: 100, lang: 'en' }); return response.data.statuses.length; } countTweetsWithHashtag('#example').then(count => console.log(`Number of tweets with #example: ${count}`));

Counting Tweets with RTweet Library

In R, you can use the 'rtweet' library. Here's a simple example:

library(rtweet) Sys.setenv(TWITTER_CONSUMER_KEY = 'your_consumer_key') Sys.setenv(TWITTER_CONSUMER_SECRET = 'your_consumer_secret') Sys.setenv(TWITTER_ACCESS_TOKEN = 'your_access_token') Sys.setenv(TWITTER_ACCESS_TOKEN_SECRET = 'your_access_token_secret') api_search <- getakka_twitterentral(lang = 'en', n = 100) count_tweets_with_hashtag <- function(hashtag) { tweets <- search_twitter(f("#{hashtag}"), API = api_search) return(nrow(tweets)) } count_tweets_with_hashtag("#example")

In each example, replace 'your_consumer_key', 'your_consumer_secret', 'your_access_token', and 'your_access_token_secret' with your actual API credentials. Remember to include the '#' sign when searching for hashtags.

Tracking the number of tweets with a specific hashtag can provide valuable insights into your social media campaigns. However, it's important to consider other metrics as well, such as engagement rate, follower growth, and sentiment analysis, to get a comprehensive understanding of your campaign's performance.

Moreover, while counting tweets can be useful, Twitter's API limitations mean the results should be taken with a grain of salt. For critical analysis, consider using Twitter's certified data partners or Twitter Analytics, which offer more reliable data but may come at a cost.

In all cases, stay ethical and follow Twitter's terms of service when accessing and using its data. Now that you know how to count tweets with a specific hashtag, start analyzing your social media campaigns today and let the data guide your strategies. Good luck!

AD