Python Script to Fetch Real Time Weather Data from OpenWeatherMap API For any City
2K views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
So in this video I will show you how to
0:04
build out a simple weather application
0:06
using the open weather map API inside
0:09
Python We will just make a simple API
0:12
request to this famous weather API So
0:15
the name of the API is open weather map
0:18
API So it's a very famous API for
0:21
getting the weather for any location So
0:24
for that you just need to create a very
0:26
uh simple account here You can create a
0:29
free
0:31
account After that you can get your own
0:34
API key So I have already created one
0:38
account here So you will need to go to
0:40
the my API key section Just grab your
0:43
own API key So just generate the API key
0:49
Just give it a
0:51
name So it will create a API key for you
0:54
and
0:56
then simply copy this and I just created
1:00
this Python script here Uh I've given
1:02
the script in the description of the
1:03
video So basically we paste this API key
1:06
and we are putting this location here
1:09
which is a city name You can put any
1:11
city in the world So I put Hakno here
1:14
and if I run this script here you will
1:16
see it will fail to retrieve data You
1:19
need to invalid API key Let me just make
1:22
sure that I paste the correct API
1:30
key So I just need to
1:36
paste So now you will see it will fetch
1:39
the temperature of the location The
1:42
weather clear sky humid humidity 20%
1:45
wind speed 2.95
1:47
millisecond So this is for luck now
1:50
location Let me change the location and
1:52
put here
1:56
Auckland And once again run this It's
1:59
you will see temperature is
2:01
13.89 Celsius weather overcast humidity
2:06
69% You can even cross check as well
2:10
Just
2:13
put so you can see it's round about
2:16
correct
2:20
So I have given this script in the
2:22
description of the video Now let me show
2:24
you step by step how to create this So
2:27
it's a very simple application We simply
2:29
make request to the open weather map API
2:32
You for that we simply require the
2:34
request module and here we specify your
2:37
API key Then we specify the actual city
2:42
and the URL to which we will make the
2:45
request
2:49
So here you'll simply make the request
2:51
to
2:52
https api open weather
2:56
map.org/data/2.5/e o rg/
3:01
data/2.5 /
3:04
weather question
3:08
mark and then Q is equal to and here you
3:12
will embed your dynamic city name and
3:15
then you also need to provide and and
3:19
then you simply need to provide the app
3:22
ID So here you will specify your API
3:27
key and the
3:29
units you will specify I want the
3:32
weather data to be in metric So this is
3:35
the overall structure of the API htt API
3:38
open weather map og data /2.5
3:45
weather and this is
3:48
specify here you will change the city
3:52
name and then you will also put your go
3:56
to the
4:07
So go to your API key section and simply
4:10
grab the API
4:14
key and paste it So after that you
4:18
simply make the request to the API So
4:20
for making the request we simply say
4:23
request dot get we make the request and
4:27
we get the data right here we convert
4:30
that data into JSON After that we
4:33
simply parse this data So we simply
4:36
compare that if the
4:38
status double equal to 200 then it's a
4:42
successful request and then we simply
4:44
grab the
4:46
data So if you try to basically print
4:49
this data here which is
4:52
coming So we simply print out the data
4:55
So this will be a simple JSON response
4:58
which will be coming if I run this
4:59
Python
5:02
script So it is saying that uh on line
5:05
number
5:06
10 there is some kind of problem here
5:09
this is status
5:14
underscore
5:16
status underscore
5:19
code So we get this JSON data here It
5:22
has all the properties longitude
5:24
latitude of the country Then we have the
5:26
weather all s So we just need to print
5:30
the relevant properties here The
5:32
properties that we want is the
5:35
temperature weather humidity and wind
5:37
speed So we will get all these four
5:40
things just store it inside the
5:42
respective variables and then we print
5:45
out one by one
5:52
So like
5:54
this So printing out the temperature the
5:57
actual weather humidity and wind speed
6:00
So if you just refresh now you will see
6:03
that you print out
6:06
12.7 weather is light rain 88% wind
6:10
speed So in this easy way you can
6:13
actually query the open weather map API
6:15
inside Python to get the weather of any
6:18
city in the world and then print out the
6:21
weather in the terminals So thank you
6:23
very much for watching this video and
6:26
please hit that like button subscribe
6:28
the channel and also check out my
6:30
website
6:31
uh
6:33
freemediatools.com uh which contains uh
6:35
thousands of tools
#Weather