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 developer.twitter.com and create a new application. It should look like this.
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 https://github.com/gitlaura/get_tweets.git $ 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 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 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 get_tweets.py [twitter_username]
$ python get_tweets.py 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.