GET Request to Server
Coddy의 Python으로 배우는 API 코스 레슨 — 10개 중 9번째.
챌린지
중급이제 API와 적절한 연결을 설정했으므로, 특정 위치에 대한 GET 요청을 보내보겠습니다.
https://wttr.in/London?format=j1로 GET 요청을 보냅니다 (?format=j1부분은 서비스에 JSON을 반환하도록 지시합니다)requests.get()객체를response라는 변수에 저장합니다response.json()을 사용하여 응답을 파싱하고data라는 변수에 저장합니다- 현재 기상 조건은
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의 모든 레슨
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results