Get followers and following list from twitter and write the data into an excel sheet using python

In this article we will use the python-twitter api to get the list of our followers and the list of people we are following. We will be writing those names to an excel sheet. The “twitter-python” library has a variety of methods. We will be dealing with only two methods in this article. Let us begin with the installation of the packages.

Install twitter-python library using pip as

$ pip install python-twitter

We also need to install one more package to work with excel sheets. Install the package by

pip install xlwt

This is the spreadsheet library that we will be using. It does not require any additional installations or modules.

API keys

The next important thing is to get the api keys. Head over this url to create an application in twitter. Once created we will get the following four keys.

  1. API key
  2. API secret key
  3. Access token key
  4. Access token secret key

Once we have all these we are good to go.

Connect to your account

The first step will be connecting to our account. The API class from twitter will be used to establish the connection. The code snippet to establish the connection will look like this.

Establishing connection using the twitter api
Establishing connection using the twitter api

It is better to keep the keys in a separate file and import here. By this way the keys will be safer. I am creating a and keeping the keys there and importing them here like this.

import keys from the constants file

The file will look like this.

consumer_key = "xxxxxxxxxxx"
consumer_secret = "xxxxxxxxx"
access_token_key = "xxxxxxxxx"
access_token_secret_key = "xxxxxxxxxx"

Once we have this let us get to the task of getting our followers list.

Get followers and following list

The GetFollowers() method can be used to get our followers list. This will return a list of our followers. We can iterate through the followers one by one to get the name of the followers.

we can also get various informations for each user like,

  1. Account created date and time
  2. Followers count
  3. Following count
  4. Name
  5. Profile picture URL and many more.

We will just get the name for now.

Twitter api to get followers list

This will print all our followers one by one. In order to get the following list we can use the GetFriends() method. This will also return a list of users and we shall iterate the list to get the name of the user.

Following list

It will be cool if we could right this data to an excel sheet right. Let us go ahead and write the names to an excel sheet. The complete code is given below.

Write twitter following list to excel sheet

This code will get a list of people that we are following and will write their names to an excel sheet. Couple of things you should know about the xlwt library are,

  1. The “add_sheet()” method takes the name of the sheet.
  2. The write method takes row, column values and the data to be written. (I am writing the names on the first column so the column will is always set to 0. I am incrementing the row value for each user)

The output of the excel sheet will look like this.

following list

We can also get a list of our followers by just changing the “GetFriends()” method in the above gist to “GetFollowes()”. If your too lazy to do that let me give you a gist also for that.

That is it. We will see some more methods in our upcoming post.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *