【发布时间】:2014-01-10 22:32:41
【问题描述】:
我正在尝试通过点击同义词 api 来创建单词的同义词列表。我正在使用 Flask 和 requests 包。
在通过烧瓶路由从网络表单中获取信息后,我只调用了一次此函数。
代码:
import requests
from flask import Flask, request, render_template, flash
import environment
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
keywords = request.form["key1"]
synonyms = syn_look(keywords)
return render_template("index.html", syns=synonyms)
return render_template("index.html")
def syn_look(word):
URL = "http://words.bighugelabs.com/api/2/%s/%s/json"
request_url = URL %(environment.thesaurus_api_key, word)
r = requests.get(request_url)
print r.status_code
if __name__ == "__main__":
app.debug = False
app.run()
状态打印两次
输出:
* Restarting with reloader
* Detected change in 'server.py', reloading
* Restarting with reloader
127.0.0.1 - - [10/Jan/2014 17:22:03] "GET / HTTP/1.1" 200 -
200
404
【问题讨论】:
-
你能给出你程序的确切输出吗?
-
{ * 使用 reloader 重新启动 * 检测到 'server.py' 中的更改,正在重新加载 * 使用 reloader 127.0.0.1 重新启动 - - [10/Jan/2014 17:22:03] "GET / HTTP/ 1.1" 200 - 200} 404 }
-
请贴出您应用程序的全部代码,尤其是您调用
syn_look方法的部分。 -
这不太可能是您提出问题的原因,但这永远不会向您显示您尝试生成的同义词。您永远不会从您正在使用的网站返回列表,因此
render_template('index.html', syns=synonyms)将等同于render_template('index.html', syns=None)。
标签: python flask python-requests