【问题标题】:Python Requests package hits url twicePython Requests 包点击 url 两次
【发布时间】: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


【解决方案1】:

您确定request_url 的格式正确吗?

您可以在 GET 请求中看到,404 打印在最后一行。这是HTTP 404 Error,找不到文件。我打赌这就是您在控制台输出中看到“重新加载”消息的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多