• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Python Basics
    • Comments
    • Functions
    • Lists
    • Loops
    • Strings
    • Syntax Basics
  • Code Examples
  • Categories
    • Basics
    • Code Snippets
    • Python On The Web
    • Scripts
    • System & OS
    • Dictionary
    • Modules In Python
    • Lists
    • Modules
    • OS


You are here: Home / Python On The Web / Tweet Search with Python

Tweet Search with Python

Last Updated: August 28, 2020

Overview

Twitter’s API is REST-based and will return results as either XML or JSON, as well as both RSS and ATOM feed formats. Public timelines can be accessed by any client, but all other Twitter methods require authentication.

About this script

The program is well documented and should be straightforward. Open up a text editor, copy & paste the code below.

Save the file as: “tweet_search.py” and exit the editor.

Getting Started

Let’s take a look at the program below that we call tweet_search.py

#!/usr/bin/python

import json
import sys
import urllib2
import os

usage = """
Usage: ./tweet_search.py 'keyword'
e.g ./tweet_search.py pythonforbeginners

Use "+" to replace whitespace"
e.g ./tweet_search.py "python+for+beginners"
"""

# Check that the user puts in an argument, else print the usage variable, then quit.
if len(sys.argv)!=2:
    print (usage)
    sys.exit(0)

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

# Set the screen name to the second argument
screen = sys.argv[1]

# Open the twitter search URL the result will be shown in json format
url = urllib2.urlopen("http://search.twitter.com/search.json?q="+screen)

#convert the data and load it into json
data = json.load(url)

#to print out how many tweets there are
print len(data), "tweets"

# Start parse the tweets from the result

# Get only text
for tweet in data["results"]:
    print tweet["text"]

# Get the status and print out the contents
for status in data['results']:
    print "(%s) %s" % (status["created_at"], status["text"])

How does it work?

Let’s break down the script to see what it does.

The script starts with importing the modules we are going to need

Line 3-6

import json
import sys
import urllib2
import os

We create a usage variable to explain how to use the script.

Line 8-14 usage = """ Usage: ./tweet_search.py 'keyword' e.g ./tweet_search.py pythonforbeginners Use "+" to replace whitespace" e.g ./tweet_search.py "python+for+beginners" """

On Line 16 we check that the user puts in an argument, else print the usage variable, then quit.



if len(sys.argv)!=2:
    print (usage)
    sys.exit(0)

Line 21-24 sets the Twitter screen name to the second argument.



screen = sys.argv[1]

Line 27 open the twitter search URL and the result will be shown in json format.



url = urllib2.urlopen("http://search.twitter.com/search.json?q="+screen)

Line 30 converts the data and loads it into json



data = json.load(url)

On Line 33 we print out the number of tweets



print len(data), "tweets"

From Line 38 we start to parse the tweets from the result

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial
for tweet in data["results"]:
    print tweet["text"]

The last thing we do in this script is to get the status and print out the contents (Line 42)



for status in data['results']:
    print "(%s) %s" % (status["created_at"], status["text"])

Go through the script line by line to see what it does. Make sure to look at it, and try to understand it.

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Code Snippets, Command Line, Python On The Web Date Originally Published: April 22, 2013

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Control Flow crawler Data Types Development Dictionary Dictionary Data Structure In Python Errorhandling Error Handling Exceptions Fabric Files ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python On The Web Python Strings Requests Scraping Scripts sh Strings System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Iterating over dictionary in Python
  • Difference between comments and docstrings in Python
  • Shortcut to comment out multiple lines in Python
  • Convert a list containing float numbers to string in Python
  • Single Line and Multi Line Comments in Python

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 ยท PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us