In this python project series(with source code) for beginners, I have made another simple real-time weather detection project in python.
In this project, we will write a simple python program to fetch the real-time weather of any city at any time. So let's get started.
Real-Time Weather Project With Python
To start first we will create a new python file and name it weather.py
Then we will import some packages for this project
First, install the python library beautifulsoup used for web scraping purposes to pull the data out of HTML and XML files.
Then install the python library requests allowing you to send HTTP requests using Python.
pip install beautiful soup
pip install requests
from bs4 import BeautifulSoup
import requests
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'}
def weather(city):
city=city.replace(" ","+")
res= requests.get(f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8',headers=headers)
print(" Searching in google......\n")
soup = BeautifulSoup(res.text,'html.parser')
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()
print(location)
print(time)
print(info)
print(weather+"°C")
print("enter the city name")
city=input()
city=city+" weather"
weather(city)
Let's try to understand the code step by step:
import the files
from bs4 import BeautifulSoup
import requests
user-agent request headers are the characteristics string that helps to server and network to identify about the applications, operating system and vendor and the version of the requesting 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'}
Write a function that defines the weather in city :
def weather(city):
city=city.replace(" ","+")
“replace” is used to replace the city name :
Using request libraries :
res = requests.get(f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8',headers=headers)
After that print, all the statements for locations, time, information about the city, and last is the weather, and then print the city name.
print("Searching in google......\n")
soup = BeautifulSoup(res.text,'html.parser')
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()
print(location)
print(time)
print(info)
print(weather+"°C")
print("enter the city name")
city=input()
city=city+" weather"
weather(city)
Now it’s time for the Output
(base) C:\Users\HP\Desktop\Projects>python weather.py
Enter the city name
Mumbai
Searching in google......
Mumbai, Maharashtra
Tuesday, 6:00 pm
Partly cloudy
34°C
Hurray! And you have successfully completed this mini python project. I hope it was helpful for you.
Stay tuned! We are coming up with more exciting python and data science projects. Don’t forget to subscribe to our newsletter to get updated on our new posts and follow our project series to get more insightful projects.
Nikita is a passionate programmer and aspiring Data scientist. She is pursuing her Master's (MCA) and is an intern at Edgrow , where she is building some exciting python projects with source code.