Skip to content

Instantly share code, notes, and snippets.

@ryosan-470
Created July 21, 2015 07:23
Show Gist options
  • Save ryosan-470/fe2f145899a1eb913241 to your computer and use it in GitHub Desktop.
Save ryosan-470/fe2f145899a1eb913241 to your computer and use it in GitHub Desktop.
Sample using json in python
import urllib.request
import json
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?q="
def kelbin2degree(d):
return d - 273.15
def get_weather(city):
with urllib.request.urlopen(BASE_URL + city) as response:
html = response.read().decode('utf-8')
return json.loads(html)
def main():
city = input(">> ")
result = get_weather(city)
status = int(result["cod"])
if status == 200:
temp_min = kelbin2degree(result['main']['temp_min'])
temp_max = kelbin2degree(result['main']['temp_max'])
get_city = result['name']
print("City:{} Temp MAX:{} MIN:{}".format(get_city, temp_min, temp_max))
else:
print("Sorry, Your input city is nothing")
main()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment