In this tutorial, you are going to learn how to use Twitter Search API with tweepy and you will know how to set-up and rolling with Tweepy. Here you'll learn from basic to advanced level query to fetch data. we'll guide you step by step from scratch.
In order to access Twitter's API you need to have the below:
Consumer API key
Consumer API secret key
Access token secret
In order to get these these keys, You'll have to create a developer account on the Twitter apps site.
Let's see some important basics concept
import tweepy auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) public_tweets = api.home_timeline() for tweet in public_tweets: print tweet.text
home_timeline() is used to fetch data from your home timeline tweets.
The above example will print tweet text to the console.
The API class provides access to the entire twitter RESTful API methods.
Each method can accept various parameters and return responses.
For more information about these methods please refer to API Reference.
When we call API Class Method the return type of all the function will be Tweepy model class instance which contain the data return from the twitter.
The return data we can use in our application
For example the following code returns to us an User model:
# Get the User object for twitter... user = api.get_user('twitter')
Models contain the data and some helper methods which we can then use:
print(user.screen_name) print user.followers_count for friend in user.friends(): print(friend.screen_name)
Pagination will be the most usable when you'll start working on twitter search API project.
pagination is useful where suppose you are 1000 tweets for a user but you want to display first 100's,200's or last 100's record then you can use pagination concept and easily can fetch data.
# Iterate through all of the authenticated user's friends for friend in tweepy.Cursor(api.friends).items(): # Process the friend here process_friend(friend) # Iterate through the first 200 statuses in the friends timeline for status in tweepy.Cursor(api.friends_timeline).items(200): # Process the status here process_status(status)
This snippet will follow every follower of the authenticated user.
authenticated user is the user whose API key is being used.
for follower in tweepy.Cursor(api.followers).items(): follower.follow()
Handling the rate limit using cursors
Since cursors raise RateLimitErrors in their next() method, handling them can be done by wrapping the cursor in an iterator.
Running this snippet will print all users you follow that themselves follow less than 300 people total - to exclude obvious spambots, for example - and will wait for 15 minutes each time it hits the rate limit.
# In this example, the handler is time.sleep(15 * 60), # but you can of course handle it in any way you want. def limit_handled(cursor): while True: try: yield cursor.next() except tweepy.RateLimitError: time.sleep(15 * 60) for follower in limit_handled(tweepy.Cursor(api.followers).items()): if follower.friends_count < 300: print follower.screen_name
We use pagination a lot in Twitter API development. Iterating through timelines, user lists, direct messages, etc.
In order to perform pagination we must supply a page/cursor parameter with each of our requests. The problem here is this requires a lot of boiler plate code just to manage the pagination loop.
To help make pagination easier and require less code Tweepy has the Cursor object.
for status in tweepy.Cursor(api.user_timeline).items(): # process status here process_status(status)
Cursor handles all the pagination work for us behind the scene so our code can now focus entirely on processing the results.
Passing parameters into the API method
What if you need to pass in parameters to the API method?
Since we pass Cursor the callable, we can not pass the parameters directly into the method. Instead we pass the parameters into the Cursor constructor method:
Now Cursor will pass the parameter into the method for us when ever it makes a request.
Items or Pages
So far we have just demonstrated pagination iterating per an item. What if instead you want to process per a page of results? You would use the pages() method:
for page in tweepy.Cursor(api.user_timeline).pages(): # page is a list of statuses process_page(page)
What if you only want n items or pages returned?
You pass into the items() or pages() methods the limit you want to impose.
# Only iterate through the first 200 statuses for status in tweepy.Cursor(api.user_timeline).items(200): process_status(status) # Only iterate through the first 3 pages for page in tweepy.Cursor(api.user_timeline).pages(3): process_page(page)