-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather-api.py
executable file
·35 lines (25 loc) · 2.18 KB
/
weather-api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
''' # The code above is a simple Python script that makes a request to the OpenWeatherMap API. It uses the requests library to make the request and prints the response to the console.
The script sends a GET request to the API endpoint https://community-open-weather-map.p.rapidapi.com/weather with the query parameters q , lat , lon , callback , id , units , mode , and lang . The query parameters are used to specify the location, units, and format of the response.
The script also sets the headers X-RapidAPI-Key and X-RapidAPI-Host to the API key and host of the OpenWeatherMap API. These headers are required by the API to authenticate the request.
To run the script, save it to a file named weather-api.py and replace YOUR_API_KEY_HERE with your actual API key. Then run the script using the following command:
python weather-api.py
The script will make a request to the OpenWeatherMap API and print the response to the console.
Conclusion
In this article, we have explored how to use the OpenWeatherMap API to get weather data for a specific location. We have covered how to get an API key, make a request to the API, and parse the response. We have also provided examples in Python and JavaScript to demonstrate how to use the API in different programming languages.
The OpenWeatherMap API provides a wealth of weather data that can be used in a variety of applications. By following the steps outlined in this article, you can easily integrate weather data into your own projects.
import requests
# make a request to the api
url = "https://community-open-weather-map.p.rapidapi.com/weather"
querystring = {"q":"London,uk","lat":"0","lon":"0","callback":"test&id=test","id":"test","units":"metric","mode":"xml","lang":"null"}
headers = {
"X-RapidAPI-Key": "YOUR_API_KEY_HERE",
"X-RapidAPI-Host": "community-open-weather-map.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
'''
import requests
# make a request to the api
response = requests.get("https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=YOUR_API_KEY_HERE")
# print the response
print(response.text)