简体   繁体   中英

Python HTTP Error 403 Forbidden

I am a bit of a Python Newbie, and I've just been trying to get some code working.

Below is the code, and also the nasty error I keep getting.

 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 

Now is the error:

 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 

Thanks for any help I can get!

The 403 error doesn't come from your code but from Google. Google lets you know that you don't have permission to access the resource you were requesting, in this case the weather API, because it has been discontinued (403 technically stands for Forbidden, they could also have gone for 404 Not Found or 410 Gone).

For more information, read http://thenextweb.com/google/2012/08/28/did-google-just-quietly-kill-private-weather-api/

Other than that, your code is correct.

It's not your error. Google discontinued its weather API a few time ago.

If you need a free weather API I would recommend using the service I'm building, Metwit weather API . This is the simplest working example using metwit-weather :

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

Further examples are found here: http://soup.metwit.com/post/45997437810/python-weather-by-metwit

I am using Python3 and having the same issue. I found that Google blocks urllib that doesnt overwrite the User-Agent and Accept-Encoding headers.

The headers it used for a test search:

GET /search?q=f1+2015 HTTP/1.1
Accept-Encoding: identity
Connection: close
User-Agent: Python-urllib/3.4
Host: 127.0.0.1:8076

I put 'Accept-Encoding' to '' and 'User-Agent' to 'testing' and the 403 error stopped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM