【发布时间】:2011-11-24 12:10:20
【问题描述】:
我正在编写一个 Python 网络应用程序,并计划在其中利用 Wikipedia。在尝试一些 URL Fetching 代码时,我能够同时获取 Google 和 Facebook(通过 Google App Engine 服务),但是当我尝试获取 wikipedia.org 时,我收到了一个异常。谁能确认维基百科不接受这些类型的页面请求?维基百科如何区分我和用户?
代码 sn-p(它是 Python!):
import os
import urllib2
from google.appengine.ext.webapp import template
class MainHandler(webapp.RequestHandler):
def get(self):
url = "http://wikipedia.org"
try:
result = urllib2.urlopen(url)
except urllib2.URLError, e:
result = 'ahh the sky is falling'
template_values= {
'test':result,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
【问题讨论】:
-
抛出了什么异常?我假设它是 urllib2.URLError 的子类,它可能会让您更好地了解问题。
-
你不能使用urlfetch api吗?它似乎对我有用。 AFAIK urllib2 默认用户代理被维基百科禁止,您无法在 gae 上更改它。
-
“一个例外”是非常无用的。向我们展示堆栈跟踪!
-
@systempuntoout 您可以更改用户代理,但不能从中删除 App ID。
-
@systempuntoout 是的,你可以。我刚刚使用 shell.appspot.com 对其进行了测试,正如预期的那样,它将
AppEngine-Google; (+http://code.google.com/appengine; appid: shell)添加到您指定的任何用户代理标头的末尾。
标签: python http google-app-engine url web-applications