1. Technology

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

By

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.

  1. About.com
  2. Technology
  3. Python
  4. Web Development
  5. RSS Reader in Python - Building an RSS Reader With Python - Python's urllib2 Module for Retrieving Web Data

©2014 About.com. All rights reserved.