我目前正在学习python,并在我的教导中到达了一节的结尾,所以我想我会尝试使用迄今为止学到的内容来构建一些基本项目。Python天气API调用问题
我发现的git这个文件,我想我会重新创建它,并稍作修改,以允许用户输入,使他们能够调整自己的城市/位置
然而,当我运行该脚本,我得到一个错误。请参阅下面的代码和下面的错误。我不知道是应该把整个错误放在这里,还是只放在最后一行,所以我认为我会在安全方面犯错,并把它放在一边。很抱歉,如果它真的很长和令人讨厌。
import urllib
import json
previous_weather_file = "weather_log.txt"
previous_weather = ""
try:
log = open(previous_weather_file, "r")
previous_weather = log.read()
log.close()
except:
print "No previous data"
city_name = raw_input("What is the city name you would like to check the weather for? ")
f = urllib.urlopen("api.openweathermap.org/data/2.5/weather?q={city_name}")
weather = f.read()
log = open(previous_weather_file, "w")
log.write(weather)
log.close()
weather_json = json.load(weather)
#print weather
#print weather_json['weather']
curr_temp = float(weather_json['main']['temp']) - 273.13
print "Temperature is %.2f degrees C" % (curr_temp)
if (not previous_weather == ""):
previous_weather_json = json.load(previous_weather)
prev_temp = float(previous_weather_json['main']['temp']) - 273.13
temp_diff = curr_temp - prev_temp
if not (temp_diff == 0.0):
print "Temperature has changed by: %.2f degrees C" % (temp_diff)
#error message
Serxhios-MBP:Projects SerxhioZefi$ python weather_get.py
What is the city name you would like to check the weather for? London
Traceback (most recent call last):
File "weather_get.py", line 16, in <module>
f = urllib.urlopen("api.openweathermap.org/data/2.5/weather?q={city_name}")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 87, in urlopen
return opener.open(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 469, in open_file
return self.open_local_file(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 483, in open_local_file
raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] No such file or directory: 'api.openweathermap.org/data/2.5/weather?q={city_name}'