GET Request to Server
الدرس 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
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results