A collection of sloppy snippets for scientific computing and data visualization in Python.

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("http://search.twitter.com/search.json?q="+query)
dict = simplejson.loads(search.read())
for result in dict["results"]: # result is a list of dictionaries
print "*",result["text"],"\n"
# we will search tweets about "fc liverpool" football team
searchTweets("fc+liverpool")

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:

searchTweets("fc+liverpool&rpp=50")

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