Погода Махабалешвара: методы получения информации о погоде с примерами кода

Чтобы получить информацию о погоде в Махабалешваре, вы можете использовать различные методы в зависимости от источника данных и языка программирования, который вы предпочитаете. Вот несколько методов с примерами кода с использованием популярных языков программирования:

  1. Использование Python и API OpenWeatherMap:

    import requests
    def get_weather():
    api_key = 'YOUR_API_KEY'
    city = 'Mahabaleshwar'
    base_url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'
    
    response = requests.get(base_url)
    data = response.json()
    
    if data['cod'] == 200:
        weather = data['weather'][0]['description']
        temperature = data['main']['temp']
    
        return f"The weather in Mahabaleshwar: {weather}, Temperature: {temperature}K"
    else:
        return "Unable to fetch weather information"
    print(get_weather())
  2. Использование JavaScript и API OpenWeatherMap:

    const fetch = require('node-fetch');
    async function getWeather() {
    const apiKey = 'YOUR_API_KEY';
    const city = 'Mahabaleshwar';
    const url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
    const response = await fetch(url);
    const data = await response.json();
    if (data.cod === 200) {
        const weather = data.weather[0].description;
        const temperature = data.main.temp;
        return `The weather in Mahabaleshwar: ${weather}, Temperature: ${temperature}K`;
    } else {
        return 'Unable to fetch weather information';
    }
    }
    getWeather().then(console.log);
  3. Использование PHP и API OpenWeatherMap:

    <?php
    function getWeather() {
    $apiKey = 'YOUR_API_KEY';
    $city = 'Mahabaleshwar';
    $url = "http://api.openweathermap.org/data/2.5/weather?q={$city}&appid={$apiKey}";
    $response = file_get_contents($url);
    $data = json_decode($response, true);
    if ($data['cod'] == 200) {
        $weather = $data['weather'][0]['description'];
        $temperature = $data['main']['temp'];
        return "The weather in Mahabaleshwar: {$weather}, Temperature: {$temperature}K";
    } else {
        return 'Unable to fetch weather information';
    }
    }
    echo getWeather();
    ?>