Monday, April 25, 2011

How to use twitter search api

The example show how to search in twitter without using third party libraries. We will use the json data-interchange format provided by twitter.
import urllib
import simplejson

def searchTweets(query):
 search = urllib.urlopen(""+query)
 dict = simplejson.loads(
 for result in dict["results"]: # result is a list of dictionaries
  print "*",result["text"],"\n"

# we will search tweets about "fc liverpool" football team
The program will print the most popular tweets about fc liverpool
* Poulsen set for return home? 

* Now: Watch live press conf - Liverpool FC 

* Who wants 20 percent off a Liverpool fc stuff 

* Liverpool FC manager Gerard Houllier in Birmingham hospital after health s... #LFC 

* RT @darsh710: Liverpool Echo: Kenny: Alberto Aquilani welcome back at LFC after Juve loan ends  #LFC @ShilChandi @KaushP @BolaAnt 

* RT @empireofthekop: Liverpool Echo : News: Kenny Dalglish: Alberto Aquilani welcome back at Liverpool FC after Juventus loan ends #LFC #fb 


  1. Is there any way using this code to get more than results?

  2. Hello Eugene,
    you can increase the number of tweets using the parameter rpp. For example, if you want 50 tweets, you have to append "&rpp=50" to the query string. You may use the function of this post calling it as follows:


    For more details you can visit the official documentation of the twitter API:

  3. Many thanks for quick reply - worked a treat!

  4. Hi thanks for this but for some reason it didn't work for me except outside the def

    >>> for tweet in data['results']:
    ... print tweet['from_user_name']
    ... print tweet['text']
    ... print tweet['to_user_name']
    ... print

    any suggestions how I can load the results in excel or make it csv? I looked everywhere and it just confused me even more thanks!

  5. you can append the resulting values into an array and save it for dumping into a file.

  6. hi now this outdated can you give recent one

  7. i am trying to use but it showing bad authentication so can u suggest.