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

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Python On The Web / Tweet Search with Python

Tweet Search with Python

Author: PFB Staff Writer
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

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.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Code Snippets, Command Line, Python On The Web Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary โ€“ How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 ยท PythonForBeginners.com

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