tempo
obter tempounknown
python
4 years ago
1.3 kB
15
Indexable
import requests
import json
id_cidade = '3477' # Cidade de São Paulo
site = 'https://www.climatempo.com.br/json/myclimatempo/user/weatherNow?idlocale=' + str(id_cidade)
try:
html = requests.get(url=site,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'})
html = html.content
except:
html = ''
print('Falha ao acessar o site')
if html:
try:
dados = json.loads(html)
cidade = ((dados[0].get('data')[0]).get('locale')).get('name')
uf = ((dados[0].get('data')[0]).get('locale')).get('uf')
temperatura = ((dados[0].get('data')[0]).get('weather')[0]).get('temperature')
humidade = ((dados[0].get('data')[0]).get('weather')[0]).get('humidity')
sensacao = ((dados[0].get('data')[0]).get('weather')[0]).get('sensation')
vento = ((dados[0].get('data')[0]).get('weather')[0]).get('windVelocity')
condicao = ((dados[0].get('data')[0]).get('weather')[0]).get('condition')
tempo = f'Local: {cidade}-{uf}\nTemperatura: {str(temperatura)}º\nCondição: {condicao}\nSensação: {str(sensacao)}º\nVento: {str(vento)} km/h\nHumidade: {str(humidade)}%'
print(tempo)
except:
print('Falha ao obter dados')Editor is loading...