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:

  1. 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.

  2. Install pip: pip is used to install python packages (a normally tedious process) extremely easily. Here’s how to install it:

    1. 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.
    2. 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).
    3. 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”).
    4. Now to test this, type “pip” in the command prompt terminal (Windows+R > cmd).

  3. Install tweepy: in the command prompt terminal, type “pip install tweepy”
  4. 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.”
  5. 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).

Advertisements

4 responses to “Working with Twitter Streams with python”

  1. Meaka says :

    Nice blog very informative. Thank you

  2. Syed Ahmed Attique says :

    bro

    i have some queries regarding your code… i am implementing your code on a part of my FYP.
    i have been searching for a way to contact you but have failed uptil now.
    kinldly contact me on ahmed2p0@gmail.com

    your help will be highly appreciated.

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: