如何使用加号在python urllib2 appengine中处理url?

时间:2013-06-08 22:05:53

标签: python google-app-engine urllib2

这是我的问题。我正在尝试从腐烂的西红柿API请求一个网址。现在问题是他们要求你让你的电影标题包含+标志,其中应该有空格。但是我不确定如何在app引擎端实现这一点,因为每当我尝试在app引擎上做同样的事情时,我都会得到同样的错误:

Traceback (most recent call last):
  File "/programming/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/programming/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/programming/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/programming/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/programming/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/programming/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/student/Desktop/Movie Rater/MovieRaterBackend/higgsmovies.py", line 12, in get
    page = urllib2.urlopen(site)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request

这是我的代码:

    title = self.request.get("title")
    site = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=" + constants.ROTTEN_TOMATOES_KEY + "&q=" + title + "&page_limit=1"
    page = urllib2.urlopen(site)
    soup = BeautifulSoup(page)
    self.response.out.write(soup)

常量只是一个包含我所有密码和东西的python文件,而我正在用漂亮的汤来清理东西,但我确信这不是问题所在。只需访问url myapplication.com/about?title=your+title+goes+here即可访问此代码,其中myapplication将成为网站的网址,可能是某些appspot.com网址。

这适用于不包含+符号的网址。

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:0)

这不会直接回答您的问题,但您是否尝试直接使用url fetch service

例如:

from google.appengine.api import urlfetch

title = self.request.get("title")
site = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=" + constants.ROTTEN_TOMATOES_KEY + "&q=" + title + "&page_limit=1"

result = urlfetch.fetch(site)

答案 1 :(得分:0)

加号(“+”)是表单数据编码标准的一部分:application/x-www-form-urlencoded

查询字符串是问号(“?”)之后的所有内容,是表单数据 - 或者,在本例中是REST查询参数。所以他们的API在这里表现正常。

答案 2 :(得分:0)

我还没有办法处理加号,因为appengine似乎推断这些是新的变量/值。但是,使用' +'以外的正则表达式只要访问URL的应用程序能够用[regex]而不是普通的' +'替换[space],这是整个问题的可行解决方案。看作该服务的预期应用是成为iPhone应用程序的后端,这种方法不应该有太多麻烦。我只需要确保我的正则表达式不包含在任何电影名称中,并且它不会太长。对于使用appengine将此类数据转发到另一个在线服务的Web应用程序,可以编写一个javascript脚本来正确处理。

答案 3 :(得分:0)

我使用urllib.urlencode

示例:

params = { 'q' : 'value', 'apikey' : key_value }

request_url += urllib.urlencode(params)

urllib2.urlopen(request_url)