J2897 icon

Get OpenWeather city names

J2897 | PRO | 05/15/24 10:38:29 PM UTC | 0 ⭐ | 1464 👁️ | Never ⏰ | []
Python |

950 B

|

None

|

0 👍

/

0 👎

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

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎