How to Fetch Twitter Tweets Using the Tweepy Library
Twitter is a valuable source of real-time information and historical data. If you're looking to access and analyze tweets, the Tweepy library provides a straightforward and efficient way to interact with the Twitter API. Tweepy is a Python library that simplifies the process of connecting to Twitter, making it easier to fetch tweets based on various criteria.
To get started, you'll first need to install the Tweepy library. This can be done using pip, the Python package
manager, with the command pip install tweepy
. Once installed, you’ll need to set up your Twitter API
credentials, which include the API key, API secret key, access token, and access token secret. These credentials are
required to authenticate your requests to Twitter's servers.
After setting up your credentials, you can use Tweepy to fetch tweets. For example, you can retrieve tweets from a
specific user, search for tweets containing certain keywords, or even gather tweets from a particular location. By
using the tweepy.Cursor
method, you can paginate through results and collect tweets according to your
needs. This flexibility makes Tweepy an essential tool for working with Twitter data.
For a comprehensive overview of the Tweepy library’s features, refer to the documentation page.
In this article, we will focus specifically on how to search for tweets using the Tweepy library.
For more information on the Twitter Search API, visit: Twitter Search API Overview.
There are three tiers available for the Twitter Search API: Standard, Premium, and Enterprise.
In this guide, we will use the Standard Twitter Search API, which is free of charge. This tier allows you to search for tweets from the past 7 days. If you need access to tweets from earlier dates, you will need to upgrade to either the Premium or Enterprise APIs, which are paid options.
User Authentication to Twitter API
Getting Application Keys and Access Tokens
First, we need to establish user authentication to access the Twitter API.
Twitter uses OAuth to ensure secure, authorized access to its API.
To generate an OAuth access token, you will need to:
- Log in to your Twitter account.
- Navigate to https://apps.twitter.com and create a new application.
- Once your new application is created, you can obtain the application's tokens and keys from the "Keys and Access Tokens" tab.
After obtaining the consumer keys and access token for your new Twitter application, you can proceed with installing the Tweepy library and writing Python code to retrieve tweets.
The following components are required in the code:
- Consumer Key (API Key)
- Consumer Secret (API Secret)
- Access Token
- Access Token Secret
Installing Tweepy Library
To install the Tweepy library, use the following pip command:
pip install tweepy
To begin using Tweepy, you'll need to install it with pip. This can be done by running the command
pip install tweepy
in your terminal. This command will download and install the Tweepy library,
making it available for use in your Python projects.
After the installation is complete, you can start incorporating Tweepy into your code to interact with the Twitter API. Ensure you have your API credentials ready, as you'll need them for authentication and accessing Twitter data.
Writing code
Next, let's create the Python code to retrieve tweets using the Tweepy library.
Installing Key and Tokens
import tweepy # consumer keys and access tokens, used for OAuth consumer_key = 'YOUR-CONSUMER-KEY' consumer_secret = 'YOUR-CONSUMER-SECRET' access_token = 'YOUR-ACCESS-TOKEN' access_token_secret = 'YOUR-ACCESS-TOKEN-SECRET' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # creation of the actual interface, using authentication api = tweepy.API(auth)
Post a new Tweet
You can use the code below to send a new tweet from your account.
api.update_status('Test')
Get information about yourself
The me()
method generates a user object and returns the profile of the user associated with the
authentication keys provided. This is useful for confirming that your API credentials are correctly set up and
for accessing user-specific details.
By calling this method, you can retrieve information about the authenticated user, such as their username, user ID, and other profile details. This is often one of the first steps in working with the Twitter API, as it allows you to verify your access and understand the data associated with your credentials.
To utilize the me()
method, ensure you have authenticated your Twitter API session properly. Once
the user object is obtained, you can proceed to interact with the API to perform actions such as posting tweets,
retrieving timelines, or searching for tweets.
user = api.me() print('Name: ' + user.name) print('Location: ' + user.location) print('Friends: ' + str(user.friends_count)) ''' Output: Name: Mavenbird Location: gujarat, India Friends: 1652 '''
Get information about other users
You can also retrieve information about other Twitter users.
The get_user()
function allows you to obtain the user object for any specific Twitter handle.
For example, let's use the Twitter user "mavenbird":
user = api.get_user('mavenbird') print('Name: ' + user.name) print('Location: ' + user.location) print('Friends: ' + str(user.friends_count)) ''' Output: Name: Mavenbird Location: Friends: 207 '''
Get friends of the authenticated user
Print 5 friends:
user_friends = user.friends() for friend in user_friends[:5]: print (friend.screen_name)
Searching/Fetching Tweets
Here, I will demonstrate how to retrieve tweets based on a specific text query.
- Search for tweets containing the text "India".
- Specify the language (Hindi language).
- Limit the search results to 5 tweets.
search = tweepy.Cursor(api.search, q="India", lang="ne").items(5)
- Search for the most recent tweets containing the text "India".
- Include tweets in any language.
- The search results can be of type: mixed, recent, or popular.
- Mixed: Includes both recent and popular tweets.
search = tweepy.Cursor(api.search, q="India", result_type="recent").items(5)
search english language tweets
search = tweepy.Cursor(api.search, q="India", result_type="recent", lang="en").items(5)
print the searched tweets
for item in search: print (item.text)
Additional tweet parameters that can be retrieved include the creation date, retweet count, user location, and more.
search = tweepy.Cursor(api.search, q="India", result_type="recent", lang="en").items(5) for item in search: # print tweet text print (item.text) # print tweet created date print (item.created_at) # print retweet count of the tweet print (item.retweet_count) # print the username who published the tweet print (item.user.name) # print the location of the user who published the tweet print (item.user.location) # print the language code of the tweet print (item.metadata['iso_language_code']) # print the search result type print (item.metadata['result_type']) # print the device/source from which the tweet has been published # e.g. Twitter for Android, Twitter for iPhone, Twitter Web Client, etc. print (item.source)
- Display a list of hashtags found in the tweet.
- Show an empty list if no hashtags are present.
for item in search: # print list of hashtags present in the tweet # print empty list when no hashtag is present hashtags = item.entities['hashtags'] if hashtags: ht = [ht['text'] for ht in hashtags] print ht else: print hashtags
- Show a list of users mentioned in the tweet.
- Display an empty list if no users are mentioned in the tweet.
for item in search: # print list of users mentioned in the tweet # print empty list if no user is mentioned in the tweet user_mentions = item.entities['user_mentions'] if user_mentions: mentions = [user['screen_name'] for user in user_mentions] # name, id, screen_name print mentions else: print user_mentions