1. Home
  2. Computing & Technology
  3. Python

Part 3: Creating a Feed Class and Finishing the Python Program

From , former About.com Guide

4 of 8

Python's urllib2 Module

Now we get to the function you have been waiting to see. How do we use the urllib2 library? Consider the code and follow my commentary below.

def getlinks (self, address):
     file_request = urllib2.Request(address)
     file_opener = urllib2.build_opener()
     file_object = file_opener.open(file_request)
     file_feed = file_object.read()

First, we define getlinks as receiving an address, a URL. Second, we use that URL to create an instance of urllib2's Request object; let's call it file_request. It is essentially a web-based form of a file, and we have simply made Python aware of its existence.

Once the web page object has been created, we can build an opener to it. For this we use urllib2's build_opener function. Just like accessing a local file requires the built-in open command, so we need to build an opener to the web-based file, establishing a connection to it. build_opener creates an object (of type OpenerDirector, for those who would like to know). That object naturally has its own attributes and methods, of which open is one. However, we have not yet opened the file itself.

When the connection has been created, we can then open the web page with the open method of the opener. We then assign that open file object the name file_object. It may then be read like any other file object. This we do here, assigning the file data, which is the RSS feed, to file_feed.

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Python

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

All rights reserved.