API Connection to Server
Coddy의 Python으로 배우는 API 코스 레슨 — 10개 중 8번째.
챌린지
중급OpenWeatherMap은 일반적으로 API 키가 필요하지만, 학습을 위해 가입이 필요 없는 무료 대안인 Open-Meteo API를 사용하겠습니다.
다음 주소로 GET 요청을 보냅니다: https://api.open-meteo.com/v1/forecast
params를 사용하여 다음 파라미터를 추가하세요:
{
"latitude": 52.52,
"longitude": 13.41,
"current_weather": "true"
}requests.get() 객체를 response라는 변수에 저장하세요
response.json()을 사용하여 응답을 파싱하고 data라는 변수에 저장하세요
data["current_weather"]["temperature"]에 접근하여 현재 온도를 출력하세요
직접 해보기
import requests
# Step 1: Send GET request with parameters
response = requests.get(
# Your URL here
params={
# Your parameters here
}
)
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Print current temperature
print("Current temperature:", data["current_weather"]["temperature"], "°C")Python으로 배우는 API의 모든 레슨
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results