在python中使用api
我试图将API与python结合使用,但我不知道我的代码哪里出了问题在python中使用api,python,Python,我试图将API与python结合使用,但我不知道我的代码哪里出了问题 import urllib.request api_key = '0275af803ebea3354695c28f9445a842' #city_name = Batman def weather(query): url = 'api.openweathermap.org/data/2.5/weather?appid=' + api_key q = query.replace(' ', '%20') final_ur
import urllib.request
api_key = '0275af803ebea3354695c28f9445a842'
#city_name = Batman
def weather(query):
url = 'api.openweathermap.org/data/2.5/weather?appid=' + api_key
q = query.replace(' ', '%20')
final_url = url + "&q=" + q + "&lang=tr"
response= urllib.request.urlopen(final_url).read()
json_obj = str(response, 'utf-8')
data = json.loads(json_obj)
for item in data['objects']:
print (item)
首先你需要导入json,关键是给我一个404错误。参见下面的工作示例<代码>数据没有下面的键
['objects']
也打印出数据的所有键,以便您可以从中选择
import urllib.request
import json
api_key = '0275af803ebea3354695c28f9445a842'
#city_name = Batman
def weather(query):
url = "https://samples.openweathermap.org/data/2.5/weather?q={}&appid=439d4b804bc8187953eb36d2a8c26a02.".format(query)
response = urllib.request.urlopen(url).read()
json_obj = str(response, 'utf-8')
data = json.loads(json_obj)
print(data)
for i in data:
print("key: ", i, "-val: ", data[i])
print(weather("London"))
请澄清你的问题。看,。只是说,你不想把你的API密钥公开给任何人