Displaying Appropriate Results
الدرس 10 من 10 في دورة API في Python على Coddy.
التحدي
متوسطالآن بعد أن نجحت في إرسال طلبات GET إلى خادم API، دعنا نعرض النتائج المحددة التي نريدها من خلال الوصول إلى قيم القاموس المتداخلة.
التعليمات:
- أرسل طلب GET إلى:
https://wttr.in/London?format=j1 - احفظ كائن
requests.get()في متغير يسمىresponse - احفظ
response.json()في متغير يسمىdata - قم بالوصول إلى الظروف الحالية، المتداخلة داخل
data["current_condition"][0]، واحفظها في متغير يسمىcurrent - احفظ
current["temp_C"]في متغير يسمىtemperature - احفظ
current["windspeedKmph"]في متغير يسمىwind - أنشئ متغيرًا يسمى
cityوقم بتعيينه إلى"London" - اطبع المتغيرات الثلاثة جميعها في رسالة منسقة
جرّب بنفسك
import requests
# Step 1: Send GET request
response = # Your code here
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Extract specific data
temperature = # Your code here
wind = # Your code here
city = # Your code here
# Step 4: Display results
print(f"City: {city}")
print(f"Temperature: {temperature}")
print(f"Wind: {wind}")جميع دروس API في Python
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results