简体   繁体   中英

Parse HTML using beautiful soup & render template in jinja2

I'm trying to parse a local HTML document using beautiful soup, then render_template() the result using jinja2.

I'm new to python, but here's what I'm trying:

@app.route("inherit/index")
def inheritIndex():
    soup = BeautifulSoup(open("templates/index.html"), "html.parser")
    soup.find(text="foobar").replaceWith("Hooray!")
    return render_template(soup)

I managed to substitute the values right from within the render_template() method. BeautifulSoup was not required. Here was my solution as suggested in the comments.

HTML:

...
<p> {{ foobar }} lorem ipsum dolor...</p>
... 

Python:

@app.route("inherit/index")
    def inheritIndex():
    return render_template("index.html", foobar="Hooray!")

    # <p> Hooray! lorem ipsum dolor...</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM