简体   繁体   English

Google App Engine:创建自定义错误页面

[英]Google App Engine: Creating a custom error page

The application that I have been working on takes a input from a user and accordingly outputs a bunch of results based on the keyword. 我一直在研究的应用程序从用户那里获取输入,并根据关键字输出一堆结果。 I use web scraping to scrap the results. 我使用网页抓取来抓取结果。

Here's the python code for the same: 这是相同的python代码:

import os
import webapp2
import jinja2
from google.appengine.ext import db
import urllib2
import re

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)
    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)
    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class MainPage(Handler):
    def get(self):
        self.render("formrss.html")
    def post(self):
        x = self.request.get("rssquery")
        url = "http://www.quora.com/" + x + "/rss"
        content = urllib2.urlopen(url).read()
        content = content.decode('utf-8')
        allTitles =  re.compile('<title>(.*?)</title>')
        allLinks = re.compile('<link>(.*?)</link>')
        list = re.findall(allTitles,content)
        linklist = re.findall(allLinks,content)
        self.render("frontrss.html", list = list, linklist = linklist)



app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

Now, since I obtain the final url by combining the keyword entered by the user, there's always a chance that the url obtained from the keyword is invalid. 现在,由于我是通过组合用户输入的关键字来获得最终的网址的,因此总是有机会从关键字获取的网址无效。 Hence, in that case, the user gets a 404 error. 因此,在这种情况下,用户会收到404错误。

Here's a live demo of the application: 这是该应用程序的现场演示:

http://quorable.appspot.com/ http://quorable.appspot.com/

How can I avoid this error? 如何避免此错误? Or rather, how can I display a custom message for the user in case the url obtained is not available? 或者更确切地说,如果获得的URL不可用,如何为用户显示自定义消息? I don't want the user to think that the application is broken or malfunctioning, when instead the keyword entered by the user is invalid. 我不希望用户认为应用程序已损坏或出现故障,而用户输入的关键字无效。

import...

def handleNone(request, response, exception):
    logging.exception(exception) # if you need some logging
    response.headers['Content-Type'] = 'text/html'
    response.write('No feed for this keywords. Go <a href="/">back</a>')
    response.set_status(404)

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

app.error_handlers[404] = handleNone

By the way, you should better do a redirect if the Quora server responds 404. Check urllib2.urlopen() reference docs : 顺便说一句,如果Quora服务器响应404,则最好进行重定向。请检查urllib2.urlopen() 参考文档

req = urllib2.Request(yourURL)
try: urllib2.urlopen(req)
except URLError as e:
    if e.code == 404:
        redirect_to_a_no_results_page() 
else:
       fetch_the_results()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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