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.
Step-by-Step Guide: How to Access Twitter Data using 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

  • Set 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 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 a 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 SecretImportant: Please save these on your local machine, we’ll be using this later.
Click on the 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 it 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.

Search Recent Tweets Using Pagination

Using the tweepy.Paginator to iterate through results. The below example shows how to retrieve recent tweets for keyword = "#WellDoneIndia", language = "English", and are not retweets. The max_results parameter specifies the maximum number of results to retrieve per page, and the limit sets the total number of pages to iterate through.

We are also able to extract additional information, specifying parameters tweet_fields, expansions, and user_fields.

Some of the info extracted includes:

  • tweet_fields: id, text, in_reply_to_user_id, conversation_id, created_at, lang, public_metrics, referenced_tweets, source.
  • expansions: author_id
  • user_fields: description, id, location, name, public_metrics, url, username, verified

Conclusion

The Twitter API is a powerful tool that can be used for a variety of purposes. Some of the most common use cases include retrieving tweets, following users, and creating or deleting tweets. But the Twitter API can be used for so much more than that!

You can also use it to get information about users, including their location, bio, and followers. You can also use it to get information about tweets, including the date and time they were sent, the number of retweets and likes they have received, and more.

The possibilities are endless when it comes to using the Twitter API.

Data Science

Pratik Sharma

Data Science ~ Machine Learning ~ Deep Learning ~ NLP ~ Generative AI

Comments


Related Posts

Data Analytics Roadmap, Interview Questions, and Tips

If you want to get into a career in data analytics, this post will help you prepare for your upcoming interview. Learn about the data analytics roadmap, common interview questions, and tips on answering them. Good luck!

Data Analytics Roadmap, Interview Questions, and Tips.

The 7 Best Books for Learning SQL

Here are the 7 best books to start learning SQL. These top-rated books will help you master basics, intermediate concepts, and advanced techniques in this powerful database language.

The 7 Best Books for Learning SQL.

The 11 Best Courses for Learning SQL

Find the best free and paid courses to learn SQL programming. These are the 11 best SQL training courses for your SQL learning journey. We will also discuss the best SQL to learn, skills needed before one can start learning SQL, and the type of jobs one can get after learning SQL.

The 11 Best Courses for Learning SQL.