MR Santy
MR Santy

Reputation: 11

How can I use the geopy library to obtain a list or a consultation of all the cities of a country

I want to get a list of cities in Colombia to enter them into a database and I found the geopy library to do this, I don't know how to do it so I need help I have the following code but it tells me that it does not support the Key error: address

import geopy
from geopy.geocoders import Nominatim

# Create a geocoder object
geolocator = Nominatim(user_agent="my-app")

# Specify the address
address = "Calle 1, Ciudad, Colombia"

# Perform geocoding
location = geolocator.geocode(address)

# Get the cities in a country
cities = [city.split(",")[-1].strip() for city in location.raw['address']['locality']]

# Print the cities
for city in cities:
    print(city)

Upvotes: 1

Views: 75

Answers (0)

Related Questions