Advertisement
Rnery

Clima tempo..

Mar 5th, 2022 (edited)
1,408
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 1 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. from requests import get
  5. from requests.exceptions import HTTPError
  6.  
  7. id_cidade = '3477' # Cidade de São Paulo
  8. site = 'https://www.climatempo.com.br/json/myclimatempo/'\
  9. 'user/weatherNow?idlocale=' + str(id_cidade)
  10.  
  11. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'\
  12. 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'}
  13.  
  14. try:
  15.     json_callback = get(url=site, headers=headers).json()
  16. except HTTPError as http_err:
  17.     print(f'HTTP erro no site: {http_err}')
  18.  
  19. if json_callback:
  20.     try:
  21.         temperatura = json_callback[0]['data'][0]['weather'][0]['temperature']
  22.         print(f"Temperatura: {temperatura}")
  23.     except Exception:
  24.         print('Falha ao obter dados')
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement