בקשת GET לשרת
שיעור 9 מתוך 10 בקורס API ב-Python של Coddy.
אתגר
בינוניכעת, לאחר שיצרת חיבור תקין ל-API, נשלח בקשת GET עבור מיקום מסוים.
- שלח בקשת GET אל:
https://wttr.in/London?format=j1(החלק?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"])כל השיעורים ביחידה API ב-Python
תרגלו בעצמכם: קומפיילר Python אונליין