Get city temperature in Python3 with requests

In this article I will teach you step by step how to get city weather information using a free 3rd party API.

Please note that the API from openweathermap is free to use for 60 API requests or less per minute. If you exceed the limit, the API requests will no longer work until the counter resets.

Steps and requirements:

  1. Create an account on https://home.openweathermap.org/users/sign_up and login
  2. Get your API key from https://home.openweathermap.org/api_keys
  3. Install requests package using pip install requests to make http requests to the weather API
  4. Now start coding

First of all, you should import the requests module and store the api key that you got from the above link and store it into a variable

import requests

API_KEY = "XXXXXXXXXXXXXXXX"

I will retrieve the current weather in London, UK so I will store the city name in a variable

city_name = "London"

In order to get the weather, we have to use the current weather API provided by openweathermap which is explained here: https://openweathermap.org/current.

The API url that we will use is http://api.openweathermap.org/data/2.5/weather?q=city_name_here

So we are creating the url by inserting the city name in the url and appending the API_KEY at the end of the url for authentication purposes:

If you have Python3.6 or above you can use the new f-strings syntax:

url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&APPID={API_KEY}"

If you have earlier versions you can use the format syntax:

url = "http://api.openweathermap.org/data/2.5/weather?q={}&APPID={}".format(city_name, API_KEY)

Now we can make the API request and parse the json response as dict with the help of the json method:

response = requests.get(url).json()

Now we have all the current London weather information plus different information like city coords, timezone, sunrise and sunset time in a dict:

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 284.2,
    "pressure": 1006,
    "humidity": 93,
    "temp_min": 283.15,
    "temp_max": 285.37
  },
  "visibility": 10000,
  "wind": {
    "speed": 5.7,
    "deg": 250
  },
  "rain": {
    "1h": 0.25
  },
  "clouds": {
    "all": 75
  },
  "dt": 1571935280,
  "sys": {
    "type": 1,
    "id": 1414,
    "country": "GB",
    "sunrise": 1571899147,
    "sunset": 1571935808
  },
  "timezone": 3600,
  "id": 2643743,
  "name": "London",
  "cod": 200
}

The key that interests us is "main" which stores the current temperature under the "temp" key.

But its value is odd: 284.2 and after a quick research I realized that the value is in Kelvin units and the formula to transform it to Celsius is temp_in_kelvin - 273.13 so we write a convert function which also use only the 2 decimals after the floating point:

def convert_kelvin_to_celsius(kelvin_temperature):
    return round(kelvin_temperature - 273.15, 2)

Now we are ready to get the temperature in Celsius and print it out by accessing the "temp" key from "main":

current_temperature_kelvin = response['main']['temp']

And using the convert function that we defined earlier, we will convert it to Celsius and print it:

current_temperature_celsius = convert_kelvin_to_celsius(current_temperature_kelvin)
print(current_temperature_celsius)

Now if you run the python file here’s what we get:

:~$ python get_temp.py
:~$ 11.05

You can extend this script to retrieve the other information from the API request.

Full code below:

import requests

API_KEY = "XXXXXXXXXXXXXXXX"
city_name = "London"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&APPID={API_KEY}"


def convert_kelvin_to_celsius(kelvin_temperature):
    return round(kelvin_temperature - 273.15, 2)


response = requests.get(url).json()
current_temperature_kelvin = response['main']['temp']
current_temperature_celsius = convert_kelvin_to_celsius(current_temperature_kelvin)
print(current_temperature_celsius)

Leave a Reply

Your email address will not be published. Required fields are marked *