1. Home
  2. Computing & Technology
  3. Python

Part 2: Getting Feed Information From the Data File With Python

From Al Lukaszewski, for About.com

3 of 9

The Tasks This Python Program Must Do

As the engine of the application, this program will need to perform the following tasks:

  • Receive user input (the name of the feed)
  • Retrieve the location of the feed (from the data file)
  • Retrieve the feed (from a web server)
  • Retrieve the some but not all pieces of information from the feed (the headlines and links)
  • Format the data for web publishing
  • Return the data to the PHP script for printing to the web page

For it to fulfill these duties, the Python program will need to borrow code from a few modules. We will need the following:

  • string : to parse the data retrieved from the data file
  • sys : to parse the user input [if CGI is used instead of PHP, then we need cgi and cgitb , remembering to call cgitb.enable(). If you feel a bit lost with this, see my tutorials on Python with PHP and Python CGI , respectively]
  • urllib2 : this module is used for opening URLs and reading from them; we will obviously use it to retrieve the feed
  • xml.dom : all RSS feeds come in XML format (it is the RSS standard), and we must be ready to parse it; for our purposes, we will be using a particular part of xml.dom called minidom (you will learn more about this later)

So with that understanding, put these import declarations at the beginning of your program:

import string
import sys
import urllib2
from xml.dom import minidom

If you are unsure about the reason for importing modules and how to do it, you should read my discussion on importing modules from the Python Library. If you would like to know more about frequently used modules in the Python Library or are unsure of what it is, see my page on the subject .

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.