Click on geonames3.py to get source.
"""
Parsing xml data from GeoNames
http://www.geonames.org/export/web-services.html
"""

import os
import io
import urllib3

from lxml import etree

def save_country_info(filename):
    url = 'http://api.geonames.org/countryInfo?username=demo'
    http = urllib3.PoolManager()
    try:
        response = http.request(
            'GET'
            ,url
            ,preload_content=False
            #,timeout=10
            )
    except urllib3.exceptions.NewConnectionError:
        print("geonames.org Connection failed")
    else:
        response.auto_close = False
        with io.TextIOWrapper(response) as download:
            with open(filename, 'wb') as f:
                for line in download:
                    print("line:",line)
                    f.write(line.encode())

def parse_country_info(filename):
    with open(filename) as f:
        tree = etree.parse(f)
    return tree


if __name__ == '__main__':
    cache_file = 'geonames.xml'
    if not os.path.exists(cache_file):
        save_country_info(cache_file)
    tree = parse_country_info(cache_file)
    root = tree.getroot()
    #countries = root.xpath('/geonames/country[population>100000000]')
    countries = root.xpath('/geonames/country[population<1000000]')
    print(type(countries))
    for country in countries:
        name = country.findtext('countryName')
        population = country.findtext('population')
        print(u'%s - Population %d' % (name, int(population)))