3

私は少し 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

私が得ることができる助けをありがとう!

4

3 に答える 3

3

それはあなたの間違いではありません。Google は数日前に天気 API を廃止しました。

無料の天気 API が必要な場合は、私が構築しているサービス Metwit weather APIを使用することをお勧めします。これはmetwit-weatherを使用した最も簡単な作業例です:

from metwit import Metwit
weather = Metwit.weather.get(location_lat=45.45, location_lng=9.18)

さらなる例はここにあります: http://soup.metwit.com/post/45997437810/python-weather-by-metwit

于 2013-05-01T12:45:42.687 に答える