维基百科是否允许通过Google App Engine获取URL?

Eri*_*oma 4 python url google-app-engine web-applications http

我正在编写一个Python Web应用程序,其中我计划利用Wikipedia.在尝试一些URL提取代码时,我能够同时获取Google和Facebook(通过Google App Engine服务),但当我尝试获取wikipedia.org时,我收到了一个例外.任何人都可以确认维基百科不接受这些类型的页面请求吗?维基百科如何区分我和用户?

代码片段(它是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))
Run Code Online (Sandbox Code Playgroud)

sys*_*out 5

urllib2默认用户代理被禁止使用维基百科,它会产生403 HTTP响应.
您应该使用以下内容修改应用程序用户代理:

#Option 1
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'MyUserAgent')]
res= opener.open('http://whatsmyuseragent.com/')
page = res.read()

#Option 2
import urllib2
req = urllib2.Request('http://whatsmyuseragent.com/')
req.add_header('User-agent', 'MyUserAgent')
urllib2.urlopen(req)

#Option 3
req = urllib2.Request("http://whatsmyuseragent.com/", 
                       headers={"User-agent": "MyUserAgent"})
urllib2.urlopen(req)
Run Code Online (Sandbox Code Playgroud)

额外链接:
高级维基百科Python客户端 http://www.mediawiki.org/wiki/API:Client_code#Python