Differences between revisions 1 and 2
Revision 1 as of 2007-09-04 23:21:36
Size: 4271
Editor: JeffRush
Comment:
Revision 2 as of 2007-09-05 09:28:36
Size: 4152
Editor: JeffRush
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
#!/usr/bin/env python
                                                                                                                                                     
Line 31: Line 29:

'''Note(s):'''

  This example uses standard Python except for
  [http://www.crummy.com/software/BeautifulSoup/ the BeautifulSoup package] which must be installed separately.

import urllib2                                                                                                                                       
from BeautifulSoup import BeautifulSoup                                                                                                              
                                                                                                                                                     
def FetchCountryCodes():                                                                                                                             
    """Country Code List: ISO 3166-1993 (E)                                                                                                          
                                                                                                                                                     
      This international standard provides a two-letter alphabetic code for                                                                          
      representing the names of countries, dependencies, and other areas of                                                                          
      special geopolitical interest. The source of this code set is the Codes                                                                        
      for the Representation of Names of Countries (ISO 3166-1993 (E)).                                                                              
    """                                                                                                                                              
                                                                                                                                                     
    page = urllib2.urlopen("http://xml.coverpages.org/country3166.html")                                                                             
    soup = BeautifulSoup(page)                                                                                                                       
    page.close()                                                                                                                                     
                                                                                                                                                     
    countries = []                                                                                                                                   
    for listrow in soup.html.body.table.findAll('tr')[1:]:                                                                                           
        col1, col2 = listrow.findAll('td')                                                                                                           
        code = col1.renderContents()                                                                                                                 
        name = col2.renderContents()                                                                                                                 
        countries.append( (code, name) )                                                                                                             
    return countries                                                                                                                                 
                                                                                                                                                     
codes = FetchCountryCodes()                                                                                                                          
print codes                                                                                                                                          

Note(s):


CategoryPythonInEducation

EduSig/DataResources/CountryCodeFetcher (last edited 2008-11-15 14:00:35 by localhost)

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