import requests
# The name of the city you want to get the weather for
city_name = "Manchester"
# Make the API request
response = requests.get(f"http://api.openweathermap.org/geo/1.0/direct?q={city_name}&limit=10&appid={OPENWEATHERMAP_KEY}")
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON data
data = response.json()
# Print the weather data for each city
for city in data:
print(f"City: {city['name']}, {city.get('state', '')} ({city['country']})")
query_city_name = f"{city['name']},{city.get('state', '')},{city['country']}"
print(f"Query: {query_city_name}")
print(f"Local names: {', '.join(city.get('local_names', {}).values())}")
print(f"Latitude: {city['lat']:.6f}")
print(f"Longitude: {city['lon']:.6f}")
print("==================================")
else:
# Print an error message
print(f"Error: {response.status_code}")
Comments
0 B
|👍
/👎