Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in

Posted on

Python Write a function to call an external api

import requests

def get_weather(city):
    Get the weather for a city.

    city : str
        The city to get the weather for.

        The weather for the city.

    #get the weather from the api
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=b6907d289e10d714a6e88b30761fae22'.format(city)
    response = requests.get(url)
    data = response.json()

    #return the weather data
    return data

weather = get_weather('London')
print(weather)  #{'coord': {'lon': -0.13, 'lat': 51.51}, 'weather': [{'id': 801, 'main': 'Clouds', 'description': 'few clouds', 'icon': '02n'}], 'base': 'stations', 'main': {'temp': 280.32, 'pressure': 1012, 'humidity': 81, 'temp_min': 279.15, 'temp_max': 281.15}, 'visibility': 10000, 'wind': {'speed': 4.1, 'deg': 80}, 'clouds': {'all': 20}, 'dt': 1560354800, 'sys': {'type': 1, 'id': 5091, 'message': 0.0039, 'country': 'GB', 'sunrise': 1560291780, 'sunset': 1560339959}, 'timezone': 3600, 'id': 2643743, 'name': 'London', 'cod': 200}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)