【发布时间】:2020-07-24 00:36:36
【问题描述】:
当我直接执行此方法时它工作正常,但当我决定创建一个将为我获取天气数据的类时出现错误。
'''
import urllib.request
import json
class weather:
def __init__(self, city, key, URL):
self.city = city
self.key = key
self.URL = "http://api.openweathermap.org/data/2.5/weather?q="
def getTemprature(self,a):
fullURL = str(self.URL+self.city+"&appid="+self.key)
data = urllib.request.urlopen(fullURL).read()
temp = float(json.loads(data)["main"]["temp"])
return temp
city="New Delhi" #default city
apiKey = "54df40e238084fbf095d3540271e48a0"
print(weather.getTemprature(city,apiKey))
'''
【问题讨论】:
标签: python json api openweathermap