Menu
Coddy logo textTech

GET Request to Server

Coddy의 Python으로 배우는 API 코스 레슨 — 10개 중 9번째.

challenge icon

챌린지

중급

이제 API와 적절한 연결을 설정했으므로, 특정 위치에 대한 GET 요청을 보내보겠습니다.

  1. https://wttr.in/London?format=j1로 GET 요청을 보냅니다 (?format=j1 부분은 서비스에 JSON을 반환하도록 지시합니다)
  2. requests.get() 객체를 response라는 변수에 저장합니다
  3. response.json()을 사용하여 응답을 파싱하고 data라는 변수에 저장합니다
  4. 현재 기상 조건은 data["current_condition"][0] 안에 있습니다. 온도(temp_C)와 설명(weatherDesc[0]["value"])을 추출한 다음, 두 가지를 모두 출력합니다

직접 해보기

import requests

# Step 1: Send GET request for London weather (j1 = JSON format)
response = # Your code here

# Step 2: Parse JSON response
data = # Your code here

# Step 3: Print weather information
current = data["current_condition"][0]
print("Temperature:", current["temp_C"], "°C")
print("Description:", current["weatherDesc"][0]["value"])

Python으로 배우는 API의 모든 레슨