【发布时间】:2021-12-08 21:54:32
【问题描述】:
使用Python,需要使用网站APIhttps://www.weatherbit.io/api显示当前天气数据。天气数据应以文本形式显示。我得到了类似的东西,但它没有按预期工作,因此它显示以下没有措施:
#Output
Enter city name : Paris
Temperature (in kelvin unit) = ['temp']
atmospheric pressure (in hPa unit) = ['pres']
humidity (in percentage) = ['rh']
description = ['description']
完整代码:
# 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))
【问题讨论】:
-
您在所有行中都忘记了
x- 它必须是x["temp"],但您只有["temp"]
标签: python api weather-api