Revision 1 as of 2007-09-05 09:30:50

Clear message

import urllib2                                                                                                            
from BeautifulSoup import BeautifulStoneSoup                                                                              
                                                                                                                          
class City:                                                                                                               
    "A simple container of arbitrary named attributes."                                                                   
    def __init__(self, **kw):                                                                                             
        self.__dict__.update(kw)                                                                                          
    def __repr__(self):                                                                                                   
        return str(self.__dict__)                                                                                         
                                                                                                                          
def FetchCityLocations():                                                                                                 
    """A Sampling of Location Information about Cities                                                                    
                                                                                                                          
       An example of fetching XML representing information about a handful                                                
       of cities along with the state within which they reside and their                                                  
       latitude/longitude.                                                                                                
    """                                                                                                                   
                                                                                                                          
    page = urllib2.urlopen("http://www.4dsolutions.net/ocn/python/cities.xml")                                            
    soup = BeautifulStoneSoup(page)                                                                                       
    page.close()                                                                                                          
                                                                                                                          
    cities = []                                                                                                           
    for city in soup.cities.findAll('city'):                                                                              
        c = City(                                                                                                         
                name=city['name'],                                                                                        
                state=city['state'],                                                                                      
                latitude="%s.%s%s" % (                                                                                    
                    city.lat['deg'], city.lat['min'], city.lat['dir']),                                                   
                longitude="%s.%s%s" % (                                                                                   
                    city.long['deg'], city.long['min'], city.long['dir']),                                                
                )                                                                                                         
        cities.append(c)                                                                                                  
    return cities                                                                                                         
                                                                                                                          
cities = FetchCityLocations()                                                                                             
print cities                                                                                                              

Note(s):


CategoryPythonInEducation

Unable to edit the page? See the FrontPage for instructions.