Python to Check Weather Forecast with Source Code

In this article, you’ll learn how to check weather forecasts using python

Prerequisites

  1. Python Basics
  2. requests module

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.

Install Necessary Modules:

Open your  Anaconda Prompt  and type and run the following command (individually):

pip install requests

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+.

Once Installed now we can import it inside our python code.

Source Code:

'''
Python Program to Check Weather Forecast
'''

# Import the necessary modules!
import requests

# Input the city name
city = input('Enter City name: ')

# Or you can also hard-code the value
# city = 'Irkutsk'

# Display the message
print('Displaying Weater report for: ' + city)

# Fetch the weater details
url = 'https://wttr.in/{}'.format(city) # Use of format to pass city as a parameter here
res = requests.get(url)                 # Make use of the requests module

# Display the result
# Resultant data is stored in res. 
# Use of the text method to extract our desired weather details to display the result
print(res.text)

Output:

Enter City name: California Displaying Weater report for: California Weather report: California \ / Clear .-. 25 °C ― ( ) ― → 11 km/h `-’ 16 km / \ 0.0 mm ┌─────────────┐ ┌──────────────────────────────┬───────────────────────┤ Sat 24 Jul ├───────────────────────┬──────────────────────────────┐ │ Morning │ Noon └──────┬──────┘ Evening │ Night │ ├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤ │ \ / Partly cloudy │ \ / Partly cloudy │ \ / Partly cloudy │ \ / Partly cloudy │ │ _ /"".-. 25 °C │ _ /"".-. +29(27) °C │ _ /"".-. 26 °C │ _ /"".-. +22(24) °C │ │ \_( ). ↗ 7-8 km/h │ \_( ). ↗ 13-15 km/h │ \_( ). → 6-7 km/h │ \_( ). ↗ 3-7 km/h │ │ /(___(__) 10 km │ /(___(__) 10 km │ /(___(__) 10 km │ /(___(__) 10 km │ │ 0.0 mm | 0% │ 0.0 mm | 0% │ 0.1 mm | 46% │ 0.0 mm | 0% │ └──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘ ┌─────────────┐ ┌──────────────────────────────┬───────────────────────┤ Sun 25 Jul ├───────────────────────┬──────────────────────────────┐ │ Morning │ Noon └──────┬──────┘ Evening │ Night │ ├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤ │ \ / Partly cloudy │ _`/"".-. Patchy light d…│ \ / Partly cloudy │ \ / Partly cloudy │ │ _ /"".-. +26(25) °C │ ,\_( ). +28(27) °C │ _ /"".-. +24(25) °C │ _ /"".-. 20 °C │ │ \_( ). ↗ 6-8 km/h │ /(___(__) ↗ 12-14 km/h │ \_( ). → 6-7 km/h │ \_( ). ↖ 5-10 km/h │ │ /(___(__) 10 km │ ‘ ‘ ‘ ‘ 8 km │ /(___(__) 9 km │ /(___(__) 10 km │ │ 0.0 mm | 0% │ ‘ ‘ ‘ ‘ 0.1 mm | 24% │ 0.3 mm | 51% │ 0.0 mm | 0% │ └──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘ ┌─────────────┐ ┌──────────────────────────────┬───────────────────────┤ Mon 26 Jul ├───────────────────────┬──────────────────────────────┐ │ Morning │ Noon └──────┬──────┘ Evening │ Night │ ├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤ │ \ / Partly cloudy │ _`/"".-. Patchy rain po…│ \ / Partly cloudy │ \ / Partly cloudy │ │ _ /"".-. +23(24) °C │ ,\_( ). 25 °C │ _ /"".-. +22(24) °C │ _ /"".-. 18 °C │ │ \_( ). ↗ 6-8 km/h │ /(___(__) ↗ 10-12 km/h │ \_( ). ↑ 9-11 km/h │ \_( ). ↙ 5-12 km/h │ │ /(___(__) 10 km │ ‘ ‘ ‘ ‘ 10 km │ /(___(__) 9 km │ /(___(__) 10 km │ │ 0.0 mm | 0% │ ‘ ‘ ‘ ‘ 0.0 mm | 26% │ 0.1 mm | 46% │ 0.0 mm | 0% │ └──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘ Location: California, United States of America [36.7014631,-118.7559973] Follow @igor_chubin for wttr.in updates
Code language: PHP (php)

Leave a Comment