1
+ from bs4 import BeautifulSoup
2
+ import requests
3
+ 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' }
4
+
5
+ #function for getting the updates and fetching the only required output
6
+ def weather (city ):
7
+ city = city .replace (" " ,"+" )
8
+ 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 )
9
+ print ("Searching......\n " )
10
+ soup = BeautifulSoup (res .text ,'html.parser' )
11
+ location = soup .select ('#wob_loc' )[0 ].getText ().strip ()
12
+ time = soup .select ('#wob_dts' )[0 ].getText ().strip ()
13
+ info = soup .select ('#wob_dc' )[0 ].getText ().strip ()
14
+ weather = soup .select ('#wob_tm' )[0 ].getText ().strip ()
15
+ #printing all the output
16
+ print (location )
17
+ print (time )
18
+ print (info )
19
+ print (weather + "°C" )
20
+
21
+ #getting the name of city from user
22
+ city = input ("Enter the Name of Any City >> " )
23
+ city = city + " weather"
24
+ weather (city )
0 commit comments