Menu
Coddy logo textTech

בקשת GET לשרת

שיעור 9 מתוך 10 בקורס API ב-Python של Coddy.

challenge icon

אתגר

בינוני

כעת, לאחר שיצרת חיבור תקין ל-API, נשלח בקשת GET עבור מיקום מסוים.

  1. שלח בקשת GET אל: https://wttr.in/London?format=j1 (החלק ?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"])

כל השיעורים ביחידה API ב-Python

תרגלו בעצמכם: קומפיילר Python אונליין