How to Download Twitter Followers or Friends for Free | by Brienna Herold | Towards Data Science

When you Google how to do this, all these companies pop up, offering to do this for you if you pay them a small fee. But Twitter gives you all the tools you need to do it yourself—for free. This short guide takes you through a Python script that helps you to use those tools.
At the end of this guide, you get a script that downloads a CSV containing all the followers and/or friends for any public Twitter handle. This file will also contain the following details about each person:
username
user id
handle
location
bio
url
followers count
friends count
date account was created
verified status
Sample CSV file for the followers of @briennakh:
Table of Contents:
Set up your Twitter developer accountGenerate authentication tokensWrite the Python scriptRun the script
Set up your Twitter developer account
Generate authentication tokens
Write the Python script
Run the script
To use the Twitter API to download bulk data such as friends and/or followers, you need a developer’s account, a project, and an app. If you already have all these, you can skip this step.
If you don’t already have a Developer’s account, get one here:
developer.twitter.com
Helpful documentation:
Developer Portal
Project
App
It may take a while to get approved, up to a few days or weeks.
If you don’t have the time to wait for approval, I would be happy to quickly get your data for you. You can find me on UpWork:
www.upwork.com
Once you have a developer’s account, you need to generate the authentication tokens that would allow you to connect to the API from a script.
In your Developer Portal dashboard, click on the key icon next to your app:
It will take you to your app page. Here, you need to generate or regenerate four pieces of information:
consumer API key
consumer API secret
access token
access secret
Immediately save these tokens to a file named config.ini, since once you finish viewing any of these, you cannot view them again. You will have to regenerate them.
Your config.ini must look like this:
[TWITTER]CONSUMER_KEY = YOUR_CONSUMER_KEY_HERECONSUMER_SECRET = YOUR_CONSUMER_API_SECRET_HEREACCESS_TOKEN = YOUR_ACCESS_TOKEN_HEREACCESS_SECRET = YOUR_ACCESS_SECRET_HERE
Swap out all the instances of YOUR_ETC_HERE with your tokens. Do not leave any blank spaces between the beginning of the file and [TWITTER].
I often use config.ini files to avoid directly pasting authentication tokens into my script, which makes it safe to make the script public, as my authentication tokens are now hidden.
To make it easier to connect to the Twitter API, we will use the Tweepy package. With this package, our script involves four steps:
Authenticate our connection.Get the Twitter id for each follower or friend.Get more details for each follower or friend, such as their handle, username, and bio. The full list can be found at the beginning of this guide.Process the data into a usable format.
Authenticate our connection.
Get the Twitter id for each follower or friend.
Get more details for each follower or friend, such as their handle, username, and bio. The full list can be found at the beginning of this guide.
Process the data into a usable format.
We’ll save this script as a file named download_friends_or_followers.py and save it to the same directory as our config.ini file.
Directory structure:
twitter-helper-scripts |_ config.ini |_ download_friends_or_followers.py
We open configs.ini and read in the tokens, and then use them to authenticate our connection to the Twitter API.
import configparserfrom tweepy import API, Cursor, OAuthHandler, TweepError# Read in configsconfigs = configparser.ConfigParser()configs.read('./config.ini')keys = configs['TWITTER']consumer_key = keys['CONSUMER_KEY'] consumer_secret = keys['CONSUMER_SECRET'] access_token = keys['ACCESS_TOKEN']access_secret = keys['ACCESS_SECRET']# Authenticate Tweepy connection to Twitter APIauth = OAuthHandler(consumer_key, consumer_secret)auth.set_access_token(access_token, access_secret)api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
To get the id for each follower of a given handle:
screen_name = 'briennakh'ids = []for fid in Cursor(api.followers_ids, screen_name=screen_name, count=5000).items():    ids.append(fid)
The process is identical for friends, except that we change the API endpoint from api.follower_ids to api.friend_ids:
screen_name = 'briennakh'ids = []for fid in Cursor(api.friends_ids, screen_name=screen_name, count=5000).items():    ids.append(fid)
Now we have ids, which is a list of follower ids, or a list of friend ids, depending on the API endpoint we used.
A list of ids is almost useless, except for requesting more data that we can actually use. So we submit these ids to api.lookup_users, the API endpoint responsible for returning details about individual users.
info = []for i in range(0, len(ids), 100):    try:        chunk = ids[i:i+100]        info.extend(api.lookup_users(user_ids=chunk))    except:        import traceback        traceback.print_exc()        print('Something went wrong, skipping...')
Here, we broke down ids into chunks of 100 ids, which is the maximum chunk size that this API endpoint accepts per request. We save the returned user details to info, extending this list every time a chunk finishes processing.
In the end, info will contain one record for each follower or friend, which looks like this:
These records are not easily usable. A different format such as CSV would be much more easily usable later on, so we process these records and download them as a CSV file.
import pandas as pddata = [x._json for x in info]df = pd.DataFrame(data)df = df[['id', 'name', 'screen_name', 'location', 'description', 'url', 'followers_count', 'friends_count', 'created_at', 'verified']]df.to_csv('followers.csv', index=False)
Note that I did not keep all possible details about each follower or friend. If you see a detail in the record that you want to add to your CSV, you can edit the code to include it. Likewise, you can also drop details that you are not interested in saving.
Furthermore, keep in mind that if you are friends with someone, and they also follow you, then this person will show up in both the followers list and the friends list. Our script does not deal with this type of post-processing.
You can copy and paste the above code into your own script, or you can find an executable version of the script here:
github.com
To use this script,
Download two files: the script and its requirements.txt. Place both files into the same directory, along with your config.ini from above. You should have 3 files in total in this directory.Using the command line, navigate to this directory. Download the Python requirements:pip install -r requirements.txtRun the script:python download_friends_or_followers.py -u [user] -t [type]For the [user] argument, enter the Twitter handle for which you want to get the followers and/or friends. This argument is required. For the [type] argument, enter the type you want: friends, followers, or both. This argument is not required. The default is followers.Example: python download_friends_or_followers.py -u briennakh -t both
Download two files: the script and its requirements.txt. Place both files into the same directory, along with your config.ini from above. You should have 3 files in total in this directory.
Using the command line, navigate to this directory. Download the Python requirements:pip install -r requirements.txt
Run the script:python download_friends_or_followers.py -u [user] -t [type]For the [user] argument, enter the Twitter handle for which you want to get the followers and/or friends. This argument is required. For the [type] argument, enter the type you want: friends, followers, or both. This argument is not required. The default is followers.Example: python download_friends_or_followers.py -u briennakh -t both
You should now see one or two CSV files appear in your directory, depending on the type you requested.
If you’d like to read more of my articles or explore millions of other articles, you can sign up for Medium membership:
brienna.medium.com
You can also subscribe to my email list to get notified whenever I publish a new article:
brienna.medium.com
Some other stories from me that might interest you: