This post will show you how to fetch and generate live weather data of any city in Python using a weather API. We are going to make use of the weather api from www.weatherapi.com . WeatherAPI is a service that provides  weather data, including realtime weather data, forecasts, and historical data to the developers of web services and mobile applications.

Before using their API, you have to signup for a free account and generate an api key here. You will need the API key for accessing their service.

We will use the python Requests module for accessing the API.

Install the requests module by running:

pip install requests

 

Fetching live weather data

Before we make the request, first we will create these variables:

  1. base_url , which stores the API's URL
  2. api_key, stores your api key
  3. city, the city whose weather data we need.
import requests

api_key = "your_API_key"
base_url = "http://api.weatherapi.com/v1"
city = "london"

parameters = {"key":api_key, "q":city}         # URL parameters
r = requests.get(f"{base_url}/current.json", params=parameters)

data = r.json()         # retrieve the json data

print(data)

output:

{'location': {'name': 'London', 'region': 'City of London, Greater London', 'country': 'United Kingdom', 'lat': 51.52, 'lon': -0.11, 'tz_id': 'Europe/London', 'localtime_epoch': 1606304125, 'localtime': '2020-11-25 11:35'}, 'current': {'last_updated_epoch': 1606302905, 'last_updated': '2020-11-25 11:15', 'temp_c': 13.0, 'temp_f': 55.4, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 8.1, 'wind_kph': 13.0, 'wind_degree': 200, 'wind_dir': 'SSW', 'pressure_mb': 1011.0, 'pressure_in': 30.3, 'precip_mm': 0.1, 'precip_in': 0.0, 'humidity': 82, 'cloud': 75, 'feelslike_c': 11.9, 'feelslike_f': 53.5, 'vis_km': 10.0, 'vis_miles': 6.0, 'uv': 3.0, 'gust_mph': 11.9, 'gust_kph': 19.1}}

 

Now lets parse this data into a nice piece

import requests

api_key = "your_API_key"
base_url = "http://api.weatherapi.com/v1"
city = "london"

parameters = {"key":api_key, "q":city}         # URL parameters
r = requests.get(f"{base_url}/current.json", params=parameters)

data = r.json()         # retrieve json

# retriving Data

location = data['location']['name']
time = data['location']['localtime']

condition = data['current']['condition']['text']     
temperature_celcius = data['current']['temp_c']
temperature_farenheit = data['current']['temp_f']
feelslike_celcius = data['current']['feelslike_c']
wind_direction = data['current']['wind_dir']


# printing data
print(f"Location: {location}")
print(f"Current Time: {time}")
print()
print(f"Weather Condition: {condition}")
print(f"Temperature in Celcius: {temperature_celcius}")
print(f"Temperature in farenheit: {temperature_farenheit}")
print()
print(f"Temperature feels like: {feelslike_celcius} Celcius")
print(f"Wind Direction: {wind_direction}")

Output:

Location: London
Current Time: 2020-11-25 11:53

Weather Condition: Light rain
Temperature in Celcius: 14.0
Temperature in farenheit: 57.2

Temperature feels like: 13.1 Celcius
Wind Direction: SSW

 

Read More: 8 Excellent Python Courses on Udemy (2021)