Skip to content

Step-by-Step Guide: How to Access Twitter Data using Python

This blog post will show you how to access Twitter data using Python. I will walk you through the steps needed to get started, and then I will show you some examples of what you can do with the data.

Pratik Sharma
Pratik Sharma
5 min read
Step-by-Step Guide: How to Access Twitter Data using Python
Steps to extract Twitter data using Tweepy (Python)

Do you want to learn how to access Twitter data using Python? In this blog post, I will show you how to do just that! I will walk you through the steps needed to get started, and then I will show you some examples of what you can do with the data. Twitter is a great source of information, and with Python, it’s easy to extract that information and use it for your purposes. So let’s get started!

How to apply for a Twitter Developer account?

There are several steps to take to get yourself registered as a developer on the site.

Pre-requisites

Pre-requisites before starting with the application of the Twitter developer account.

  1. Twitter account 😉
  2. Verified email address and phone number

Detailed Steps

Twitter Developer Account Form
  1. Apply to the Twitter Developer Account.
  2. On the next screen, fill few basic details about the use case and the country in which you’re based.
  3. Agree to Developer agreement & policy
  4. Once that’s done, you’ll get an email from Twitter to verify the developer account application.

You can check some of the frequently asked questions (FAQs) on developer account management, access levels, and functionality here.

Create a Project and connect an App

  • Setup up a project using this link. You would be asked to provide the project name, use case (similar to what you did while applying for the developer account), and a project description.
Create a Project in Twitter Developer Account
  • Once you’ve finished the preceding steps, you need to next create an App. It will be within the project you created in the previous step. Important: the name of the app you’re creating must not be duplicated or else you may receive an error.
  • On the next screen, you’ll be presented with keys & tokens i.e. API Key, API Key Secret, and Bearer Token. Important: Please save these on your local machine, we’ll be using this later.
  • Now, let’s set up authentication to sign in to the app with Twitter.
User authentication settings while applying for Twitter Developer Account
  • There are two types of authentication methods, you can read about them in detail here.
  • There are three types of app permissions on the user authentication settings page. (i) Read, (ii) Read and write (iii) Read and write and direct message. Depending on your use case, please select the appropriate app permission on the User authentication settings page.
  • Please read about callback URLs.
  • Once you’ve provided callback URLs and a website for the app, depending on the OAuth method, you should see:
Screen after successful authentication setting applied if OAuth 1.0a was selected as the authentication method.
  • Please remember, if you selected OAuth 2.0, you would need to store the additional credentials apart from the ones generated above.
Screen after successful authentication setting applied if OAuth 2.0 was selected as the authentication method.
  • Let’s now generate Access Token and Secret. Important: Please save these on your local machine, we’ll be using this later.
Click on Generate button next to Access Token and Secret.

Twitter API access levels

Twitter API v2 includes a few access levels to help you scale your usage on the platform. In general, new accounts can quickly sign up for free, Essential access. Should you want additional access, you may choose to apply for free Elevated access and beyond. For a detailed understanding of different types of access levels, you can use this link.

What is Tweepy and how to install Tweepy?

Tweepy is a Python library that allows users to access data from Twitter’s API. Tweepy can be used for a variety of purposes, such as getting information on tweets, filtering data by certain keywords, and more. You can install Tweepy using pip or install directly from the GitHub repository.

## using pip way
pip install Tweepy

## cloning from GitHub repo
pip install git+https://github.com/tweepy/tweepy.git

Using Tweepy to access Twitter data

Please note that for the examples explored below, I use the “Elevated access” method and therefore have the following limitations:

  • Project limits: 1 Project
  • App limits: 3 Apps per Project
  • Tweet caps: Retrieve up to 2 million Tweets per month

Authenticating the app

We’ll use the API key (consumer key), API key secret (consumer secret), bearer token, access token, and access token secret saved in previous steps here to authenticate the app using Tweepy.

Analyzing Tweets

A lot can be done here e.g. hide or unhide a reply to a tweet, like or unlike a tweet, get a list of users who have liked a tweet, get liked tweets of a user, create or delete a tweet, retweet or un retweet a tweet, etc. Let’s have a look at this one by one.

Creating a Tweet & Deleting it!

## Create Tweet
# Replace your tweet text instead of "Hey everyone!"
client.create_tweet(text = "Hey everyone!")

## Delete Tweet
# Replace the id of the tweet you want to delete
client.delete_tweet(1492601786842361856)
Creating a Tweet and deleting it!

Search Recent Tweets

Let’s examine the search recent tweets endpoint, which returns seven days’ worth of results that match the search query provided. Let’s say we want to check for recent tweets with a keyword = "#WellDoneIndia", language = "English", and are not retweets.