Using the new Twitter Search API (v1.1) requires a little bit more work than in the previous version. Today, I am going to show you how to set everything up and finally show you how to use this new Twitter Search API to search whatever you want on Twitter. I am doing this because many people have asked me to do it!
Using the new Twitter Search API – Requirements
Before you can use this new version of the Twitter Search API, you need a few things – just two things to be exact and they include:
- Create a Twitter Application on Twitter Developer Site
- Install one of the Twitter Libraries for the respective language like in our example, Python!
The first step is pretty straight-forward to do and I have actually saved you a ton of reading by creating a short video that you can quickly follow to create a Twitter Application on Twitter Developer Page. Please use it to setup and obtain the keys and tokens that will be generated when you create your first application.
The second part is the fun one although not very simple. In order to install the TwitterSearch Library – a Python API for Twitter, you can use either easy_install or pip. I have, fortunately, created straight-to-the point guides on how to install both of them on your computer. I even show you how to use them. To install pip, click here and if you prefer easy_install, click here. Once you have installed either one, simply go to your command line and do: easy_install TwitterSearch or pip install TwitterSearch and press enter. Depending on which one you installed, you should be able to use that TwitterSearch library in your Python code. You can install easy_install by watching this video I recently created 🙂
The Twitter Search API Code Example
When you created an application on Twitter Developer Site in step 1 above, you generated the following:
- OAUTH_TOKEN (Access Token)
- OAUTH_SECRET (Access Token)
Now is the time to use these bad boys! This is going to be fun! By the way, keep them private because well, they are your secrets.
What we will be searching for on Twitter
Let us search for something we rarely think about; what happy people say! You know, there is so much stress in the world, and so why not see what happy people talk about? You can try the opposite of happiness later.
#using twitter search api
from TwitterSearch import *
searchOrder = TwitterSearchOrder() # create a TwitterSearchOrder object
searchOrder.setCount(10) # only return 10 pages
searchOrder.setIncludeEntities(False) # and don't give us all those entity information
# Now let's create a Twitter Search API Object here
# complete these by copying from your Twitter Application
# from Twitter Developer Site
ts = TwitterSearch(
consumer_key = 'xxxxxxxxx',
consumer_secret = 'yyyyyyyy',
access_token = 'zzzzzzzz',
access_token_secret = 'aaaaaaaa'
for happy_tweet in ts.searchTweetsIterable(searchOrder):
print( '@%s tweeted: %s' % ( happy_tweet['user']['screen_name'],
happy_tweet['text'] ) )
except TwitterSearchException as e: # deal with exceptions as usual here
The Results of Running the above Twitter Search Code
- @dudewitch tweeted: happy flavour day to all my heroes
- @RobertBrooks26 tweeted: I had 6 dogs at my house all weekend. That made me happy, but I’d still prefer 6 cats or something
- @Ima_YellowGirl tweeted: Happy I never had to “act” like I had.
- @ErrinBear tweeted: RT @cedesziegler: Rain. I’m happy.
- @Tia_Milan95 tweeted: I guess I’ll tell skinny happy birthday too .!
- @DevonSeacord tweeted: Why waste your time getting hurt by someone, when there’s someone else out there waiting to make you happy?
- @avernalaw tweeted: @ozsultan @gagnier @saurnou Happy Labor Day. Are we on for Wednesday?
- @Meredith_anne1 tweeted: RT @anna_smitty: happy hell week may the eggs be ever in your favor
- @wilaisingkhon tweeted: RT @ItsLifeFact: Nobody can make you happy until you’re happy with yourself first.
- @AwkwardComedy tweeted: RT @FactsGuide: Depressed people are likely to get colds more often while happy and energetic individuals get sick less often.
- @FlyestPenguin tweeted: RT @shannamalcolm: It’s Labor Day so I can sleep late right? Good morning & happy Labor Day!:)
You see, people are having fun out there! You might have also noticed in your own results that the smiley face is associated with happiness – but when I tried searching the emoticon itself, I got errors because of encoding issues.
Where to go from here
The best place to stop by from here is the Twitter Search API documentation and read more about other methods you can use to analyze Twitter data. Once again, try searching for sad tweets or what you want or think about on a daily basis. This is just the tip of the twitter-berg (not an iceberg)!
So the real difference between the new twitter search api and the older one is that you have to have authentication to access the data! That is the bottom line.
If you liked this post, please share and if you have any questions, ask them through the comments. Subscribe for more updates as well!Improve this idea by creating a real application of your own! The sky is the limit.