Skip to content

Commit 26771c6

Browse files
authored
Create weather.py
1 parent 5093244 commit 26771c6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

weather.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
def weather(city):
6+
city=city.replace(" ","+")
7+
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)
8+
print("getting weather data ....\n")
9+
try:
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+
weather_in_one_word = soup.select('#wob_dc')[0].getText().strip()
14+
weather = soup.select('#wob_tm')[0].getText().strip()
15+
print(location)
16+
print(time)
17+
print(weather_in_one_word)
18+
print(weather+"°C")
19+
except Exception:
20+
print("Oops , something wrong occured , Make sure u have entered a valid city name :)")
21+
22+
while(True):
23+
print("\n \nenter City Name")
24+
city=input()
25+
city=city+" weather"
26+
weather(city)

0 commit comments

Comments
 (0)