
Introduction
Hello friends, this is the article that gives an idea of how to get weather forecast using Python and the code of how to generate the weather report. The weather has become a very important aspect of our day-to-day activities. Our daily activities are mostly dependent on how the weather outside. Whether it’s cloudy, raining, or extremely hot or cold weather report gives us information about it. As we leave the house unexpectedly, it starts raining. The ‘weather forecast report ‘ helps us to know what would be today’s weather forecast to solve these problems.
This is a very simple and interesting code for beginners. It helps us to get the weather report by writing simple lines of code in python. The comments provided in this help us to understand the code line by line.
You can check our website for more articles related to python and its projects.
If you want only code, please go to the bottom of the page. Copy the code from there using the copy button.
1. Starting with the import function
from bs4 import BeautifulSoup import requests
Here we have imported the Beautiful Soup library from the package(bs4) and requests module. The requests module helps to integrate your Python programs with web services. The Beautiful Soup module is designed to make screen-scraping get done quickly.
2. Setting User-Agent
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
The User-Agent request header is a character string. It lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.
3. Requesting information from the URL
res=requests.get(f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0. i635i39l2j0l4j46j690.6128j1j7&sourceid=chrome&ie=UTF-8',headers=headers)
The request function allows you to get the information from the URL and store it in the object i.e res.
3. Using the soup function
soup = BeautifulSoup(res.text,'html.parser')
We use BeautifulSoup to navigate our website and extract the data and store it in soup.
4. Extracting the information
location = soup.select('#wob_loc')[0].getText().strip() time = soup.select('#wob_dts')[0].getText().strip() info = soup.select('#wob_dc')[0].getText().strip() weather = soup.select('#wob_tm')[0].getText().strip()
Here we can extract the information about location, time, info, and weather. It can be done by removing all the extra spaces with the help of the ‘strip’ function.
4. Enter the data to get the weather report
print("enter the city name") city=input() city=city+" weather" weather(city)
Enter the city name and pass the city concatenated with the weather. It is passed to the weather function to extract the weather information of that particular city.
Complete Code to Get weather forecast using Python
#Weather report #Importing Beautifulsoup Library from bs4 import BeautifulSoup #importing requests module import requests #header user agent is a string allows the server to identify the O.S and application headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} #defining the weather function def weather(city): # Replaces the space with + operator city=city.replace(" ","+") #requests and get function to get the information from the URL provided res=requests.get(f'https://www.google.com/search?q={city}&oq={city}’ f'&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=' f'chrome&ie=UTF-8',headers=headers) #searches the information from google print("Searching in google......\n") #Navigates on that particular website ,extract and store the data in soup object soup = BeautifulSoup(res.text,'html.parser') #gets the information of location location = soup.select('#wob_loc')[0].getText().strip() #gets the information of time time = soup.select('#wob_dts')[0].getText().strip() #gets the desired information info = soup.select('#wob_dc')[0].getText().strip() #gets the weather information weather = soup.select('#wob_tm')[0].getText().strip() #prints location print(location) #prints time print(time) #prints info print(info) #prints the weather in degree celcius print(weather+"°C") #enter the city name print("enter the city name") city=input() #Concatenating the city name and weather city=city+" weather" #passing the city object to weather function weather(city)
Output

Thank you for reading our article.
Also Read
- Top 25 Pattern Programs in C++
- Currency Converter in C++
- SQLite | CRUD Operations in Python
- Number Guessing Game in C++
- Image background remover in Python
- C++ Project Structure
- Python | Check if a string is a palindrome or not without Recursion
- Python | Check if a number is an Armstrong Number or not using Recursion
- Python | Check if a number is an Armstrong Number or not without using Recursion
- Python | Shuffle a list using recursion
- Python | Shuffle a list without recursion
- Python | Implementing switch case using functions
- Python function to check whether a number is perfect or not
- Python | Find LCM using function
- Python | Find HCF using function
- Python | Convert the binary number to decimal without using library function
- Python | Create a basic operations calculator(+, -, /, and x), create a separate function for each operation
- Python | Detecting the number of local variables declared in a function
- Python | Making a chain of function decorators (bold italic underline etc)
- Python | Access function inside a function
- Event Management System Project in Python
- ATM machine program in C++
- Python | Create a function with a pass statement
- Python | Function to calculate the square root of a number
- Python | A function that calculates the power of a number