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 apps.twitter.com 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’.

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 https://github.com/gitlaura/get_tweets.git
$ cd get_tweets

Step 3: Add your credentials to get_tweets.py

Open up get_tweets.py 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 get_tweets.py
consumer_key = "[your_consumer_key]"
consumer_secret = "[your_consumer_secret]"
access_key = "[your_access_key]"
access_secret = "[your_access_secret]"

Step 4: 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 get_tweets.py [twitter_username]

Example:

$python get_tweets.py getlaura

Step 5: 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.

CSV Delimiter: Right now the delimiter for CSV file that we write to is ‘|’. You can update this to a comma or whatever character you’d like on line 36.

Name of the new CSV file: You can create a csv file with whatever name you want. Edit this code –  {0}_tweets.csv”.format(username)  – the first parameter of the open method on line 35.

Leave a Reply

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