3

我用Bottle做了一个简单的程序。然而,我遇到了一个错误;

@route('/login')
def login_form():
 return '''<form method="POST" action="/login">
            What do you want to search for?<br>
            <input name="search" type="text" />
            <input type="submit" />
          </form>'''

@route('/login', method='POST')
 def PeopleSearch():
  raw = request.forms.get('search')
  query = urllib.urlencode ( { 'q' : 'who is '+raw } )


response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query   ).read()                           
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]

for result in results :
    url = result['url']
    return (url)

进口是:

# -*- coding: cp1252 -*-
import codecs
from bottle import route, run, template, request
import Tkinter
from Tkinter import *
import turtle
import time
from xgoogle.search import GoogleSearch, SearchError
import urllib2
import urllib
import json as m_json
 import sys
from urllib import FancyURLopener
import time
from BeautifulSoup import BeautifulSoup
import json as simplejson

Soup = BeautifulSoup
from nltk import sent_tokenize, word_tokenize
from collections import Counter
from math import log10
# -*- coding: utf-8 *-*

现在这个程序应该返回 url,但它没有。它也抛出了我之前提到的错误。当我不在网页上运行它时,它工作得很好。

4

2 回答 2

0

-- 抱歉 -- 我看到你bottle的帖子上有标签。也许你还没有request从瓶子里导入模块。确保你有:

from bottle import request, get

在脚本的开头。我真的很喜欢这个网页(http://bottlepy.org/docs/dev/tutorial.html)的瓶子:)


你有没有导入urllib.request库,看起来你可能没有?尝试在开始时导入它:

import urllib.request

文档中有关此模块的大量信息:http: //docs.python.org/3.4/library/urllib.request.html

于 2013-07-20T04:36:09.227 回答
0

看起来您缺少request模块导入。

你的脚本顶部有这个吗?

from bottle import request
于 2013-07-20T04:39:27.150 回答