forked from Airon752/airon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherForecast.py
69 lines (49 loc) · 1.85 KB
/
weatherForecast.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import json
import os
from bs4 import BeautifulSoup
from unicodedata import normalize
with open('states.json') as file:
states = json.load(file)
def removeAccents(txt):
return normalize('NFKD', txt).encode('ASCII', 'ignore').decode('ASCII')
def getState():
txt = input("State: ").replace(" ", "").lower()
state = removeAccents(txt)
return states[state] if state in states else None
def getCidade():
txt = input("City: ").replace(" ", "").lower()
return removeAccents(txt)
def printWeather(soup):
title = soup.find_all("title")[0].get_text()
if title != 'Previsão do Tempo para - - Tempo Agora':
print('\n')
print(title.replace("Previsão do Tempo para", "Weather Forecast for"))
print("Temperature: ", soup.find_all("span")[5].get_text())
print("Thermal sensation: ", soup.find_all("span")[7].get_text())
print("Wind Speed: ", soup.find_all("span")[9].get_text())
print("Pressure: ", soup.find_all("span")[11].get_text())
print("Humidity: "+soup.find_all("span")[13].get_text())
else:
print("City not Found")
def weather():
url_base = "http://www.tempoagora.com.br/previsao-do-tempo"
while True:
os.system('clear')
print("Checking the weather forecast on tempoagora.com.br")
print("Input the data")
state = getState()
if state:
city = getCidade()
else:
print("State not Found\n\n\n")
url = "{}/{}/{}/".format(url_base, state, city)
site = requests.get(url)
printWeather(BeautifulSoup(site.content, 'html.parser'))
continuar = ''
while continuar != 'y' and continuar != 'n':
continuar = input("Do you wish to continue? [Y/n]: ").lower()
if continuar == 'n':
return
if __name__ == '__main__':
weather()