蟒蛇| Flask Url_For MissingSchema:无效的 URL 错误

时间:2021-06-28 11:25:41

标签: python flask beautifulsoup sqlalchemy

我在尝试从用户所在的当前页面抓取 html 时遇到问题。本质上,用户正在构建练习列表以创建锻炼程序,用户从 Select 字段中进行选择,并且每次他们单击“添加”按钮,它将填充他们迄今为止选择的列表。然后我将从该列表中获取文本并将其与我的数据库中的内容相匹配 我的问题出现在 requests.get(url_for('createARoutine'))

requests.exceptions.MissingSchema:无效的 URL '/createaroutine':未提供架构。也许你的意思是 http:///createaroutine?

使用直接 url "http://127.0.0.1:5000/createaroutine" 测试时,我的错误更改为 werkzeug.routing.BuildError: 无法为端点 'http://127.0.0.1 构建 url :5000/createaroutine'。您是指“createARoutine”吗?

@app.route("/createaroutine", methods=["GET", "POST"])
def createARoutine():
    """
    present form to creatine new routine, each time user clicks to 
    add an exercise, show the exercise to the side"""
    form = CreateRoutineForm()
    query = Exercises.query.all()
    choices = [(c.id, c.name) for c in query]
    form.exercises.choices = choices

    # collect all exercises and add to routine, 
    # also add routine to users favorites
  
    if request.method == 'POST':
        this_html = requests.get(url_for('createARoutine'))     <----ERROR
        soup = BeautifulSoup(this_html, 'html.parser')
        p = soup.find_all("li", {"id": "exerciseChoices"})
        print(p)
        return redirect(url_for('showWorkoutRoutines'))
    return render_template("createRoutine.html", form=form)

`

1 个答案:

答案 0 :(得分:0)

这行不通,因为 requests 需要一个完全限定的 URL,包括您正在运行的服务器。

另一方面,我很难理解你为什么要做你正在做的事情!您使用 get 调用您自己的站点,而不是从您所在的位置访问数据。这是一个可怕的想法。您拥有确切功能所需的所有数据。如果您需要 html,它在模板中。你永远不应该做你在这里做的事情。