How to Use a Python Script to Download Tweets with the Twitter API

I’m excited to share a step-by-step guide to set up a python script that allows you to download any Twitter user’s tweets. This is a good first script or a tutorial for using an API since Tweepy makes this really easy.

Step 1: Get Your Twitter API Credentials

Go to and create a new application. It should look like this.

Create a Twitter Application

If all went well, you should have just created a Twitter application. Next, click on the ‘Keys And Access Tokens’ tab. At the top you’ll see your consumer tokens. Scroll to the bottom and click ‘Create my access token’.


Now you have the four access keys you’ll need to run our get_tweets script. The four tokens you care about are:

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

Step 2: Get the python script from github

Now we’re going to clone one of my git repositories that contains the python script we need. On your command line enter the following:

$ git clone
$ cd get_tweets

Step 3: Make sure you have tweepy

Tweepy powers this script. If you haven’t installed it before you need to install it. You can use this command if you use pip.

$ sudo pip install tweepy

Step 4: Add your credentials to

Open up in your text editor. In lines 11-15 you need to replace the placeholder text with your new Twitter Application API keys.

# lines 11-15 of
consumer_key = "[your_consumer_key]"
consumer_secret = "[your_consumer_secret]"
access_key = "[your_access_key]"
access_secret = "[your_access_secret]"

Step 5: Run the script from the command line

Let’s run the script and download the last 100 tweets from any Twitter user. Make sure you’re in the correct folder and enter the following at the command line:

$ python [twitter_username]


$ python getlaura

Step 6: Optional setup

Number of tweets: I built this script to get the last 100 tweets a user sent. If you want to change that number, you can set the variable ‘number_of_tweets’ on line 25 equal to any number you want.

Name of the new CSV file: You can create a csv file with whatever name you want. Edit this code outfile = username + _tweets.csv on line 34.

10 thoughts on “How to Use a Python Script to Download Tweets with the Twitter API

  1. Hi,

    Thanks for this wonderful explanation. Can you please help edit this to get all the tweets that show up on twitter with a particular hashtag? for example, this script lets me get all tweets published by an account like @realDonaldTrump. But, how can I edit it to get all tweets which contain #trump?

    Thanks in advance!

  2. Hi there, and many thanks for this tutorial. Unfortunately when I try to run the script I’m prompted with an error message, any idea what could be the issue?

    It’s a syntax error concerning line 34
    print “writing to {0}_tweets.csv”.format(username))

    I’m using python 3.5. and thanks in advance for your help.

  3. Do you have more resent instructions? I get an invalid syntax error on line 13 even though I do a straight copy/paste of the keys right from twitter apps/tokens tab.

    Is there an easier way to grab tweets now? these instructions are 5 years old.

    • I just updated the script and the instructions. Let me know if it works for you now! I am using Python 2.7 so that may be the issue.

Leave a Reply to Michael Cancel reply

Your email address will not be published.