【问题标题】:Python HTTP Error 403 ForbiddenPython HTTP 错误 403 被禁止
【发布时间】:2013-04-27 08:41:06
【问题描述】:

我是一个 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

感谢您的帮助!

【问题讨论】:

  • 请根据 Pythonic 约定格式化您的代码,然后我们可能会为您提供帮助。

标签: python http urllib weather-api google-weather-api


【解决方案1】:

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/

除此之外,您的代码是正确的。

【讨论】:

    【解决方案2】:

    这不是你的错误。 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

    【讨论】:

      【解决方案3】:

      我正在使用 Python3 并遇到同样的问题。我发现 Google 会阻止不会覆盖 User-Agent 和 Accept-Encoding 标头的 urllib。

      它用于测试搜索的标题:

      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
      

      我将 'Accept-Encoding' 设置为 '' 并将 'User-Agent' 设置为 'testing' 并且 403 错误停止了。

      【讨论】:

        猜你喜欢
        • 2016-06-20
        • 2018-05-15
        • 2011-07-12
        • 1970-01-01
        • 2014-08-16
        • 2017-01-26
        • 1970-01-01
        • 2018-01-14
        • 2018-01-02
        相关资源
        最近更新 更多