-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather1.py
31 lines (26 loc) · 1.05 KB
/
weather1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 get_weather(city):
city = city+" weather"
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("\nSearching...\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()
weather = str(round((int(weather)-32)*5/9, 2))
print(location)
print(time)
print(info)
print(weather+"°C")
return_list = [str(weather+"°C"), info, time]
print(return_list)
return return_list
#city = input("Enter the Name of City -> ")
#city = city+" weather"
#get_weather(city)