Forecast Weather using Python

Proxlight
3 min readAug 23, 2021

--

Forecast Weather using Python By Proxlight

According to Wikipedia

Weather is the state of the atmosphere, describing for example the degree to which it is hot or cold, wet or dry, calm or stormy, clear or cloudy. On Earth, most weather phenomena occur in the lowest layer of the planet’s atmosphere, the troposphere, just below the stratosphere. Weather refers to day-to-day temperature, precipitation, and other atmospheric conditions, whereas climate is the term for the averaging of atmospheric conditions over longer periods of time. When used without qualification, “weather” is generally understood to mean the weather of Earth.

Hi everyoneđź‘‹ , In this article, we will learn how to forecast weather details. We will see the implementation in Python with hardly a few lines of code.

You can refer to my YouTube video to see a working tutorial for better understanding and a step-by-step guide of the same.

What will be covered in this Blog

1.    What is wttr?
2. What is requests Module
3. How to forecast the weather using Python

Let’s get started!

What is wttr?

wttr — the right way to check the weather!

wttr.in is a console-oriented weather forecast service that supports various information representation methods like terminal-oriented ANSI-sequences for console HTTP clients (curl, httpie, or wget), HTML for web browsers, or PNG for graphical viewers.

wttr.in uses wego for visualization and various data sources for weather forecast information.

If you wish to know more about it, you can refer to wttr’s GitHub Repo.

Module Used: requests Module:

Requests is a simple, yet elegant HTTP library. It allows you to send HTTP/1.1 requests extremely easily. Requests officially support Python 2.7 & 3.5+.

If you wish to know more about it, you can refer to Requests Module Documentation.

Now that you are familiar with Requests Module basics and have acquired basic knowledge of wttr, we can move forward to the coding section.

Time to Code!

You can find all the code in my GitHub Repository. Drop a star if you find it useful.

In order to access the Python library, you need to install it into your Python environment

pip install requests

Now, we need to import the package into our python script. Use the following command to do so.

import requests

Now that we have imported the library using the command import requests, let's proceed.

Let’s ask the user to input the city name for which he/she wishes to fetch the weather details.

city = input('input the city name')
print(city)

You can also hard-code the value if you will only check for yourself.

city = 'Delhi'

Now, let’s display a simple message.

print('Displaying Weather report for: ' + city)#output:
Displaying Weather report for: Delhi

Let’s define the URL, We will make use of format to pass city as a parameter here.

url = 'https://wttr.in/{}'.format(city)

It’s time to make use of the requests module.

res = requests.get(url)

Our resultant data is stored in res. We will make use of the text method to extract our desired weather details and let's display the result.

print(res.text)

So, at the end here is how our code will look like :

Screenshot showing the final code

And here is the final output :

Isn’t it beautiful? And with that, it’s a wrap! I hope this article was helpful for you if it is the leave your comments below. I will meet you in another article until then KEEP CODING🤗.

--

--

Proxlight

Welcome to Proxlight, a place for all to learn coding and fun stuff.