Differences between revisions 3 and 4
Revision 3 as of 2007-09-05 10:59:59
Size: 4160
Editor: JeffRush
Comment:
Revision 4 as of 2008-11-15 14:00:35
Size: 4162
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 33: Line 33:
  [http://www.crummy.com/software/BeautifulSoup/ the BeautifulSoup package] which must be installed separately.   [[http://www.crummy.com/software/BeautifulSoup/|the BeautifulSoup package]] which must be installed separately.

   1 import urllib2                                                                                                                                       
   2 from BeautifulSoup import BeautifulSoup                                                                                                              
   3                                                                                                                                                      
   4 def FetchCountryCodes():                                                                                                                             
   5     """Country Code List: ISO 3166-1993 (E)                                                                                                          
   6                                                                                                                                                      
   7       This international standard provides a two-letter alphabetic code for                                                                          
   8       representing the names of countries, dependencies, and other areas of                                                                          
   9       special geopolitical interest. The source of this code set is the Codes                                                                        
  10       for the Representation of Names of Countries (ISO 3166-1993 (E)).                                                                              
  11     """                                                                                                                                              
  12                                                                                                                                                      
  13     page = urllib2.urlopen("http://xml.coverpages.org/country3166.html")                                                                             
  14     soup = BeautifulSoup(page)                                                                                                                       
  15     page.close()                                                                                                                                     
  16                                                                                                                                                      
  17     countries = []                                                                                                                                   
  18     for listrow in soup.html.body.table.findAll('tr')[1:]:                                                                                           
  19         col1, col2 = listrow.findAll('td')                                                                                                           
  20         code = col1.renderContents()                                                                                                                 
  21         name = col2.renderContents()                                                                                                                 
  22         countries.append( (code, name) )                                                                                                             
  23     return countries                                                                                                                                 
  24                                                                                                                                                      
  25 codes = FetchCountryCodes()                                                                                                                          
  26 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.