该项目是一个简单的网络爬虫和搜索引擎。“索引”处理程序具有用于输入要搜索的域和要查找的术语的表单。我希望 POST 方法重定向到“LuckySearch”处理程序,该处理程序搜索正则表达式术语。
我试过使用 web.redirect() 和 web.seeother(),但似乎这些函数不支持字符串替换。我还能如何解决这个问题?
class Index(object):
def GET(self):
form = searchform()
return render.formtest(form)
def POST(self):
form = searchform()
if not form.validates():
return render.formtest(form)
else:
word = form['word'].get_value()
print "You are searching %s for the word %s" % (form['site'].get_value(), word)
raise web.redirect('/%s') % word
class LuckySearch(object):
def GET(self, query):
query = str(query)
lucky = lucky_search(corpus, query)
ordered = str(pretty_ordered_search(corpus, query))
if not lucky:
return "I couldn't find that word anywhere! Try google.com instead."
else:
return "The best page is: " + lucky + "\n" + "but you might also try:" + "\n" + ordered
class About(object):
def GET(self):
return "This is my first search engine! It only runs on my local machine, though."
if __name__ == "__main__":
corpus = crawl_web('http://en.wikipedia.org/wiki/Trinity_Sunday', 'http://en.wikipedia.org/wiki/Trinity_Sunday')
app = web.application(('/', 'Index', '/about', 'About', '/(.*)', 'LuckySearch'), globals())
app.run()