How to use weather API to get weather data in python 3


Python 3 Weather API
Intermediate

In this article we are going to discuss about how we can use weather API in python 3 to get weather data. For this task we can use various types of weather APIs that are available based on either satellite data, observatory data, or even crowdsourced data. Including weather data in your next python 3 program will help to add extra value or may even be a core part of your application.

Overview Of Weather APIs

Here is an overview of most popular Weather APIs used in Python. To make easy to understand let’s use a table.

API NameFree / PaidFeatures
1. OpenWeatherMap Freemiumcurrent weather
weather forecasts
historical weather
weather stations
weather alerts
2. Weatherbit Freemium Current Weather Data
5 Day Forecast
Severe Weather Alerts
48 Hour Forecast
16 Day Forecast
3. AccuWeather free trial/
paid plans
Forecast
Current Conditions
Alerts
Imagery
4. Dark Sky FreemiumCurrent weather conditions
Minute-by-minute forecasts
Hour-by-hour and day-by-day forecasts
Hour-by-hour and day-by-day observations
Severe weather alerts in some locations
6. ClimaCell Freemium Realtime, Short Term and Hourly Forecasts

For continue this article I am choosing Python Open Weather Map API since,

  • Can use basic feature for free
  • Create many API keys
  • Can get data about current weather, weather forecasts, historical weather, weather stations, weather alerts

To make easy for the reader to understand, I have divided this explanation into few steps. Here is an overview of those steps.

Overview of Steps

  1. Install pyowm (Python Open Weather Map API)
  2. Import pyowm to our project
  3. Get API Key
  4. How to configure pyowm easily
  5. Get current Weather
    • Get Temperature data
    • Get Humidity data
    • Get Wind data
    • Get Cloud data
    • How to access more data
  6. Get Forecast  Weather
    • Forecast with no specific time
    • Forecast for specific time

Step 1 : Install pyowm (Python Open Weather Map API)

To get weather data we use python pyowm (Python Open Weather Map) in our code. This was released under MIT License (MIT). Let’s install this API to begin our task. First of all, I assume that you have already installed python 3 in your computer. For installation of pyowm we use PIP (Package Installer for Python). Let’s create a folder named PythonWeather. Then create a file name main.py in that folder. After setting those things we are going open our Terminal (Linux / Mac) , Command Prompt (Windows) or Integrated shell in Visual Studio Code. Since I am using Visual Studio Code I use Integrated Power Shell in Visual Studio Code. You are free to use Terminal or Command Prompt. After opening shell, navigate your PythonWeather folder, which was created above.

[ shell ]
pip install pyowm

OUTPUT

Step 2 : Import pyowm to our project

In previous step we have successfully installed pyowm. In this step we are going to import pyowm to out project to use its functions. First open main.py in your favorite editor and add following line into main.py

[ insert to main.py ]
import pyowm # import Python Open Weather Map to our project.
[ shell ]
python main.py #run python script

OUTPUT

Step3 : Get Weather API Key

To get data from API we need to get an API key for this go to https://openweathermap.org/

  • Then signup,
  • Make sure you have confirmed email,
  • After sign up go to section called API keys in your dash board.
  • There you can get API key. Copy it.
  • After registering you have to wait 10 minute – 2hours until key activates and go to next step .

Step 4 : How to configure pyowm easily

In this step we are going to configure pyown API to get data. First, we need to store our API key to a variable. Then, we have to give that key to OWM( … ) function in pyown and get return object. After getting the returning object, we need to say the location using weather_at_place( … ) function. After telling the location it will return the weather object. Finally we have to use get_weather( … ) to fetch Weather data from their servers.

[ insert to main.py ]
APIKEY='32 bit hexadecimal'                  #your API Key here as string
OpenWMap=pyowm.OWM(APIKEY)                   # Use API key to get data
Weather=OpenWMap.weather_at_place(‘London’)  # give where you need to see the weather
Data=Weather.get_weather()                   # get out data in the mentioned location

Step 5 : Get current Weather in Python

Get Temperature

Weather object have a function named ‘get_temperature( … )’ to get temperature. We can get temperature both in Celsius and Fahrenheit. This function returns Average Temperature, Minimum Temperature and Maximum Temperature.

[ insert to main.py ]
temp = Data.get_temperature(unit='celsius')      # get current temparature in celsius 
print ("Average Temp. Currently ", temp['temp']) # get avg. tmp
print ("Max Temp. Currently ", temp['temp_max']) # get max tmp
print ("Min Temp. Currently ", temp['temp_min']) # get min tmp>>
[ shell ]
python main.py #run python script

OUTPUT

Get Humidity

Weather Object allows us to get humidity value using ‘get_humidity()’ as shown in below. It returns humidity as an integer.

[ insert to main.py ]
humidity = Data.get_humidity() # get current humidity 
print ("Humidity : ",humidity) # print humidity 
[ shell ]
python main.py #run python script

OUTPUT

Get Wind

We can get both wind speed and wind direction in degrees using ‘get_wind()’ function in weather object as shown below.

[ insert to main.py ]
wind = Data.get_wind() # get current wind 
print ("Wind Speed : ",wind['speed']) # print wind speed
print ("Wind Direction in Deg : ",wind['deg']) # print wind Direction
[ shell ]
python main.py #run python script

OUTPUT

Get Cloud

Weather object has information about cloud coverage percentage of a given city. We can access these information using ‘get_clouds()’ function. This will return as a numeric value.

[ insert to main.py ]
cloud = Data.get_clouds() # get current cloud 
print ("Cloud Coverage Percentage : ",cloud) # print cloud coverage percentage
[ shell ]
python main.py #run python script

OUTPUT

In the above examples in step five I only showed few methods.  There are more methods which we can use to get various kind of weather data, some of them are mentioned below.

Data.get_sunset_time() # get sunset time
Data.get_sunrise_time() # get sunrise time
Data.get_pressure() # get pressure data 
Data.get_status() # get status of current weather    eg: Rain
Data.get_detailed_status() # get status with more detailed  eg: light rain

For More Refer to : https://pyowm.readthedocs.io/en/latest/pyowm.weatherapi25.html

Step 6 : Get Forecast  Weather

[ insert to main.py ]
Weatherforecast = OpenWMap.three_hours_forecast('London') 
# give the city where you need to forecast

Part One : Forecast (No Specific Time)

This example shows how we can access weather forecast data for no specific time. It gives us True/False values for related weather conditions.

[ insert to main.py ]
rain=Weatherforecast.will_have_rain() # forecast rain
sun=Weatherforecast.will_have_sun() # forecast sun
cloud=Weatherforecast.will_have_clouds() # forecast clouds

print("There will be rain :",rain) # print details
print("There will be sun :",sun) #print details
print("There will be clouds :",cloud) # print details
[ shell ]
python main.py #run python script

OUTPUT

Part Two : Forecast (With Specific Time)

Also we can get weather forecast data for specific times. This example shows how we can do this.

[ insert to main.py ]
time = '2020-08-03 16:30:00+00'

rain=Weatherforecast.will_be_rainy_at(time) # forecast rain
sun=Weatherforecast.will_be_sunny_at(time) # forecast sun
cloud=Weatherforecast.will_be_cloudy_at(time) # forecast clouds

print("There will be rain :",rain) # print details
print("There will be sun :",sun) #print details
print("There will be clouds :",cloud) # print details
[ shell ]
python main.py #run python script

OUTPUT

Conclusion

Step by step we have reached to the end of article I hope that this article was helpful to you. Moreover I have provided links to some documentations. You can learn more on this topic using those documentations. Thank You !

Subscribe to our newsletter

Get new tips in your inbox automatically. Subscribe to our newsletter!

Leave a Reply

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

Recent Content