Я хочу получить только несколько частей ответа API и распечатать их.
Когда я запускаю это:
def get_weather(lat,lon):
import requests
r = requests.post('https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&exclude=hourly,daily&appid=92d93ccc6ac5587d35d3ccc4479083a1'.format(lat,lon))
print(r.text)
get_weather(33,44)
ответ API отображается как:
{"lat":33,"lon":44,"timezone":"Asia/Baghdad","timezone_offset":10800,"current":{"dt":1604279014,"sunrise":1604287381,"sunset":1604326320,"temp":295.15,"feels_like":290.63,"pressure":1015,"humidity":35,"dew_point":279,"uvi":4.16,"clouds":0,"visibility":10000,"wind_speed":5.1,"wind_deg":140,"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}]}}
Running: python (cwd=C:\Users\Daddy\Desktop\Cisco Problems\weatherForecast.py pid=6528). Exited with code=0 in 0.64 seconds.
Поскольку мне нужны только dt, temp, погода, влажность и скорость ветра, я добавил операторы печати в свой код:
def get_weather(lat,lon):
import requests
r = requests.post('https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&exclude=hourly,daily&appid=92d93ccc6ac5587d35d3ccc4479083a1'.format(lat,lon))
print(r["dt"])
print(r["temp"])
print(r["weather"])
print(r["humidity"])
print(r["wind_speed"])
get_weather(33,44)
И теперь я получаю эту ошибку:
TypeError: 'Response' object is not subscriptable