我使用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,但事实并非如此。它也抛出我之前提到的错误。当我不在网页上运行时,它运行得非常好。
答案 0 :(得分: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
答案 1 :(得分:0)
看起来你错过了request
模块导入。
你的脚本顶部是否有这个?
from bottle import request