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.
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:
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',
You can check that you’ve authenticated by running:
Method used to get all Tweets for user is called
GetUserTimeline, you can see all available options via:
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)
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:
Originally published at www.alexkras.com on November 19, 2016.