Работа с API сайта https://www.weatherbit.io/api
Нужно вывести текущие данные о погоде с использованием API сайта https://www.weatherbit.io/api
Данные о погоде должны быть выведены в текстовом виде.
# import required modules
import requests, json
# Enter your API key here
api_key = "API_key"
# base_url variable to store url
base_url = "https://api.weatherbit.io/v2.0/current?"
# Give city name
city_name = input("Enter city name : ")
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
# get method of requests module
# return response object
response = requests.get(complete_url)
# json method of response object
# convert json format data into
# python format data
x = response.json()
# store the value corresponding
# to the "temp" key of y
current_temperature = ["temp"]
# store the value corresponding
# to the "pressure" key of y
current_pressure = ["pres"]
# store the value corresponding
# to the "humidity" key of y
current_humidity = ["rh"]
# store the value of "weather"
# key in variable z
z = ["weather"]
# store the value corresponding
# to the "description" key at
# the 0th index of z
weather_description = ["description"]
# print following values
print(" Temperature (in kelvin unit) = " +
str(current_temperature) +
"\n atmospheric pressure (in hPa unit) = " +
str(current_pressure) +
"\n humidity (in percentage) = " +
str(current_humidity) +
"\n description = " +
str(weather_description))
Но результат выходит пустой без температурных значений:
Enter city name : Moscow
Temperature (in kelvin unit) = ['temp']
atmospheric pressure (in hPa unit) = ['pres']
humidity (in percentage) = ['rh']
description = ['description']
Ответы (1 шт):
Автор решения: Санобар Акбарова
→ Ссылка
Вы должны изучить как указать индексы json данных.
Рекомендую посмотреть: https://www.youtube.com/watch?v=oQfNYqz8pLs .
Я сделала так:
json_data = requests.get(url).json()
series = json_data['data']
for i in series:
print(i)
Далее, отдельно выбираете, что вам нужно.Например температура:
series2 = i['temp']
print("Температура: "+ str(series2) + 'ºC')
Результат выглядить так: Температура: 11ºC