Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. def get_weather(city):
  2.     search_url = "https://www.metaweather.com/api/location/search/?query=" + city
  3.     result = requests.get(search_url)
  4.     data = result.json()
  5.     if not data:
  6.         print("city not found")
  7.         return
  8.     cityID = data[0]['woeid']
  9.     weather_url = "https://www.metaweather.com/api/location/" + str(cityID)
  10.     result = requests.get(weather_url)
  11.     data = result.json()
  12.     #print(data['consolidated_weather'][1]['the_temp'])
  13.    
  14.     for element in data['consolidated_weather']:
  15.         # print(element['applicable_date'] + '     '+ str(element['the_temp']))
  16.         text = "In {city} on {date} the temperature is {temp}".format(city = city , date = element['applicable_date'], temp = element["the_temp"])
  17.         print(text)
  18.  
  19. cities = ["Munich", "Hamburg", "Dubai", "Berlin", "Bangkok"]
  20.  
  21. for element in cities:
  22.     get_weather(element)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement