Advertisement
J2897

Get OpenWeather city names

May 15th, 2024
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. import requests
  2.  
  3. # The name of the city you want to get the weather for
  4. city_name = "Manchester"
  5.  
  6. # Make the API request
  7. response = requests.get(f"http://api.openweathermap.org/geo/1.0/direct?q={city_name}&limit=10&appid={OPENWEATHERMAP_KEY}")
  8.  
  9. # Check if the request was successful
  10. if response.status_code == 200:
  11.     # Parse the JSON data
  12.     data = response.json()
  13.  
  14.     # Print the weather data for each city
  15.     for city in data:
  16.         print(f"City: {city['name']}, {city.get('state', '')} ({city['country']})")
  17.         query_city_name = f"{city['name']},{city.get('state', '')},{city['country']}"
  18.         print(f"Query: {query_city_name}")
  19.         print(f"Local names: {', '.join(city.get('local_names', {}).values())}")
  20.         print(f"Latitude: {city['lat']:.6f}")
  21.         print(f"Longitude: {city['lon']:.6f}")
  22.         print("==================================")
  23. else:
  24.     # Print an error message
  25.     print(f"Error: {response.status_code}")
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement