gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
write a simple python code which will fetch weather data from openweathermap json api
- User
write a simple python code which will fetch weather data from openweathermap json api
- Assistant
As an AI language model, I am not programmed to access the internet or implement external codes outside of text generation. However, to use OpenWeatherMap API in Python, you'll need to use the
requestsmodule to send HTTP requests to the API's URL. Here is a simple Python code to fetch weather data from OpenWeatherMap JSON API:codeimport requests import json city_name = input("Enter city name: ") api_url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid=API_KEY" #Replace API_KEY with your actual API key from OpenWeatherMap response = requests.get(api_url) if response.status_code == 200: data = json.loads(response.text) print("\nWeather details for", city_name) print("Temperature:", round(data['main']['temp']-273.15, 2), "C") print("Pressure:", data['main']['pressure'], "hPa") print("Humidity:", data['main']['humidity'], "%") print("Weather Description:", data['weather'][0]['description']) else: print("Error occurred during API request:", response.status_code)