Working with Twitter Streams with python
So here’s how to work with Twitter streams using python (in Windows). Note that I’ll be working on python 2.7.8 here:
Install python: I’d already done this, so I can’t describe this step-by-step. But as far as I remember, it was pretty straightforward, installing it like any other application is installed after downloading the python executable from the Download section of this webpage.
Install pip: pip is used to install python packages (a normally tedious process) extremely easily. Here’s how to install it:
- Download pip from here. If you’re working on Chrome, rightclick the “get-pip.py” link, and select “Save Link As”, as opposed to just clikcing on it. Ensure it is saved as a .py file.
- Just opening the installed file will cause a python script to run, and pip will be installed (along with setuptools, if setuptools has not already been installed).
- Most people don’t seem to need this, but I had to add the folder containing python scripts to my Path variable. To do this in Windows 8.1, go to the start menu, type in “System Environment”, clikc on “Environment Variables”, edit the “Path” field under system variables and append “;C:\Python27\Scripts” (or “;whatever\your\python\path\is\Scripts”).
Now to test this, type “pip” in the command prompt terminal (Windows+R > cmd).
- Install tweepy: in the command prompt terminal, type “pip install tweepy”
Setup Twitter account to access the live 1% stream: To access the live 1% stream, do the following (steps taken from Coursera course “Introduction to Data Science” ‘s first offering’s first assignment):
- Create a twitter account if you do not already have one.
- Go to https://dev.twitter.com/apps and log in with your twitter credentials.
- Click “create an application”
- Fill out the form and agree to the terms. Put in a dummy website if you don’t have one you want to use.
- On the next page, scroll down and click “Create my access token”
- Click “Create my access token.”
Test things out! : Follow a few users, and wait until a few tweets are visible on your home timeline. Then run the “Hello Tweepy” program, and if it works, and you see your home timeline’s tweets, you’re all set! (Be sure to replace the consumer_key, consumer_secret, access_token and access_token_secret fields with their appropriate values first).