How to Get User Feed with Twitter API and Python

Last week I wanted to scrape my Tweets for the past few days. It was pretty straightforward to do with Python and I wanted to share my notes.

Side note: I must say that Python is AWESOME! Even though I have not written a line of production code in Python, my life has change since I’ve read Automate the Boring Stuff with Python. Now Python is my goto language of choice (sorry JavaScript) anytime I want to crunch some data or automate a task.

Install Python Twitter

For the purpose of this article, I am assuming you have Python installed (all Macs do for example) and know how to access it from your terminal.

I’ve used a library called Python Twitter which you can get via regular pip install python-twitter.

Generate Access Token

Once this is done you will need to get:

  • Consumer Key (API Key)
  • Consumer Secret (API Secret)
  • Access Token
  • Access Token Secret

You can get all 4 by heading over to https://apps.twitter.com.

Once there, sign in with your Twitter account and click on “Create New App” button.

Fill in required information (note that app name must be unique) and select “Create Your Twitter Application”.

You will be taken to your application view. There click on “Keys and Access Tokens” tab. Look for section called Token Actions and click on “Create my Access Token”. The page should refresh, and if everything went well you should see both Consumer Key/Secret and Access Token/Secret.

Get the Tweets

Now that the boring part is done, we can do the fun stuff.

Open up your Python console, by running python in your terminal. Or I highly recommend using iPython, which is a drop in replacement for Python console, but way better. You can get it via pip install ipython and then running ipython in your terminal.

In any case, first thing you’ll need to do in your Python terminal is to import python-twitter library via:

import twitter

Next you’ll need to authenticate with Twitter via the following command, making sure to fill in the Consumer and Token information that you obtained earlier:

api = twitter.Api(consumer_key='FILL-ME-IN',
consumer_secret='FILL-ME-IN',
access_token_key='FILL-ME-IN',
access_token_secret='FILL-ME-IN')

You can check that you’ve authenticated by running:

print api.VerifyCredentials()

Method used to get all Tweets for user is called GetUserTimeline, you can see all available options via:

help(api.GetUserTimeline)

In my case I just wanted to get last 10 Tweets. My Twitter username is akras14 BTW. So I ran the following:

t = api.GetUserTimeline('akras14', count=10)

This will return a list (a.k.a. Array in JavaScript) of “Status” objects, something internal to python-twitter library, where every Status represents a tweet. Since I just wanted to grab my data, I converted it to regular Dictionaries (a.k.a Objects in JavaScript).

The following command uses something called list comprehension which is just a hipster way of doing a for loop on every Tweet, converting it to a Dictionary via built in “AsDict” method, and storing all of the converted Tweets into a List.

tweets = [i.AsDict() for i in t]

That is really it, now all of the Tweets can be printed out for inspection:

for t in tweets:
print t['text']

Enjoy 🙂


Originally published at www.alexkras.com on November 19, 2016.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.