Oct. 09, 2012

Tweet Search using Python


This script will use the Twitter API to search for tweets. 

Getting Started

Since we will be using JSON for this, don't forget to import that module. 

The screen name in Twitter, is the screen name of the user for whom to return
results for. 

Open the twitter search URL, the result will be shown in json format. 

We can get the number of tweets using the length function. 

At the end of the script, we parse the tweet from the result and print it to 
#Importing the modules
import urllib2
import json

screen_name = "wordpress"

url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" + screen_name

data = json.load(urllib2.urlopen(url))

print len(data), "tweets"

for tweet in data:
    print tweet['text']

To make it more interactive, you can ask the user for which tweet to search for:
screen = raw_input("Which tweet do you want to search? ")

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.