我是一个 Python 新手,我一直在尝试让一些代码工作。
下面是代码,也是我不断遇到的令人讨厌的错误。
import pywapi import string google_result = pywapi.get_weather_from_google('Brisbane') print google_result def getCurrentWather(): city = google_result['forecast_information']['city'].split(',')[0] print "It is " + string.lower(google_result['current_conditions']['condition']) + " and " + google_result['current_conditions']['temp_c'] + " degree centigrade now in "+ city+".\n\n" return "It is " + string.lower(google_result['current_conditions']['condition']) + " and " + google_result['current_conditions']['temp_c'] + " degree centigrade now in "+ city def getDayOfWeek(dayOfWk): #dayOfWk = dayOfWk.encode('ascii', 'ignore') return dayOfWk.lower() def getWeatherForecast(): #need to translate from sun/mon to sunday/monday dayName = {'sun': 'Sunday', 'mon': 'Monday', 'tue': 'Tuesday', 'wed': 'Wednesday', ' thu': 'Thursday', 'fri': 'Friday', 'sat': 'Saturday', 'sun': 'Sunday'} forcastall = [] for forecast in google_result['forecasts']: dayOfWeek = getDayOfWeek(forecast['day_of_week']); print " Highest is " + forecast['high'] + " and "+ "Lowest is " + forecast['low'] + " on " + dayName[dayOfWeek] forcastall.append(" Highest is " + forecast['high'] + " and "+ "Lowest is " + forecast['low'] + " on " + dayName[dayOfWeek]) return forcastall
现在是错误:
Traceback (most recent call last): File "C:\Users\Alex\Desktop\JAVIS\JAISS-master\first.py", line 5, in <module> import Weather File "C:\Users\Alex\Desktop\JAVIS\JAISS-master\Weather.py", line 4, in <module> google_result = pywapi.get_weather_from_google('Brisbane') File "C:\Python27\lib\site-packages\pywapi.py", line 51, in get_weather_from_google handler = urllib2.urlopen(url) File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 400, in open response = meth(req, response) File "C:\Python27\lib\urllib2.py", line 513, in http_response 'http', request, response, code, msg, hdrs) File "C:\Python27\lib\urllib2.py", line 438, in error return self._call_chain(*args) File "C:\Python27\lib\urllib2.py", line 372, in _call_chain result = func(*args) File "C:\Python27\lib\urllib2.py", line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 403: Forbidden
感谢我能得到的任何帮助!
最佳答案
403 错误不是来自您的代码,而是来自 Google。 Google 让您知道您无权访问您所请求的资源,在本例中是天气 API,因为它已被停用(403 在技术上代表 Forbidden,它们也可能代表 404 Not Found 或 410 Gone ).
有关更多信息,请阅读 http://thenextweb.com/google/2012/08/28/did-google-just-quietly-kill-private-weather-api/
除此之外,您的代码是正确的。
关于Python HTTP 错误 403 禁止访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250050/