tweepy Getting started with tweepy


Tweepy is a Python wrapper for the Twitter API. It accesses the Twitter REST (including Search) and Stream APIs.

Read more about the Twitter APIs, Tweepy documentation, or check out Tweepy on GitHub.

The current version of Tweepy is 3.5.0.


Tweepy can be installed from its PyPI repository using pip or easy_install:

pip install tweepy


easy_install tweepy

You can also download the source from GitHub and install it using

python install

See the tweepy documentation for more.

Using Tweepy to access the Twitter Search API

The Search API provides access to recent tweets*. This is as opposed to the Stream API, which provides search results in real-time.


*Note that "the Search API is focused on relevance and not completeness" - Twitter Search API

Using Tweepy to access the Twitter Stream API

The Stream API provides access to tweets in real-time. Streams can be filtered based on keywords, language, location, and more. Here's a simple example to track mentions of the word "tweepy":

#set up a new class using tweepy.StreamListener

class SimpleListener(tweepy.StreamListener):
    def on_status(self, status): 
        #code to run each time the stream receives a status

    def on_direct_message(self, status): 
        #code to run each time the stream receives a direct message
    def on_data(self, status):
        #code to run each time you receive some data (direct message, delete, profile update, status,...)

    def on_error(self, staus_code):
        #code to run each time an error is received
        if status_code == 420:
            return False
            return True

#initialize the stream

tweepy_listener = SimpleListener()
tweepy_stream = tweepy.Stream(auth = api.auth, listener=tweepy_listener())

You can track different keywords by changing the track parameter.

<to add: examples of filtering based on locations, languages, etc.>

You can track data adressed to your account by using userstream() instead of filter.