python - 是否有更短的方法或 pythonic 方法来生成遵循使用 BeautifulSoup 模式的自定义 html?

标签 python html python-3.x beautifulsoup

我正在构建 HTML 作为一个更大项目的一部分。建筑工程,没有问题。但是我担心代码太冗长或者我没有使用 BeautifulSoup 的全部功能。

例如:我正在生成一个 div类(class)标签editorial包装了一个 div类(class)editorial-title , editorial-image , editorial-subtitle , editorial-article以该顺序。

示例 HTML-

<div class="editorial">
    <div class="editorial-title">Hello</div>
    <div class="editorial-image"><img src="https://images.dog.ceo/breeds/collie-border/n02106166_2595.jpg"></div>
    <div class="editorial-subtitle">world</div>
    <div class="editorial-article">Yeah. But Parasite? It should have been Gone with the Wind!</div>
</div>

这是适用于我正在尝试做的小型演示版本的长代码 -

from bs4 import BeautifulSoup

title = "Hello"
subtitle = "world"
image_url = "https://images.dog.ceo/breeds/collie-border/n02106166_2595.jpg"
article = "But Parasite? It should have been Gone with the Wind!"

editorial_container = BeautifulSoup('', 'html.parser')
editorial_container_soup = editorial_container.new_tag('div', attrs={"class": "editorial"})

editorial_soup = BeautifulSoup('', 'html.parser')

editorial_title = editorial_soup.new_tag('div', attrs={"class": "editorial-title"})
editorial_image = editorial_soup.new_tag('div', attrs={"class": "editorial-image"})
image = editorial_soup.new_tag('img', src=image_url)
editorial_subtitle = editorial_soup.new_tag('div', attrs={"class": "editorial-subtitle"})
editorial_article = editorial_soup.new_tag('div', attrs={"class": "editorial-article"})

editorial_title.append(title)
editorial_image.append(image)
editorial_subtitle.append(subtitle)
editorial_article.append(article)

editorial_soup.append(editorial_title)
editorial_soup.append(editorial_image)
editorial_soup.append(editorial_subtitle)
editorial_soup.append(editorial_article)

editorial_container_soup.append(editorial_soup)
editorial_container.append(editorial_container_soup)
print(editorial_container.prettify())

它可以完成工作,但我觉得它太长了。有没有更优雅的方法来实现这一目标?

最佳答案

对于您正在执行的任务,我强烈考虑使用 Jinja模板而不是 BeautifulSoup。

如果你使用 Jinja,你只需要将带有编辑信息的字典传递给 editorial.html看起来像这样:

<!-- reusable editorial.html -->
<div class="editorial">
    <div class="editorial-title">{{ title }}</div>
    <div class="editorial-image"><img src="{{ image }}"></div>
    <div class="editorial-subtitle">{{ subtitle }}</div>
    <div class="editorial-article">{{ article }}</div>
</div>

包括 editorial.html在下面的 html 文件中,它会被 flask 加载。在此示例中,这将用作您的基本模板。
<!-- template.html -->
<html>
    <head>
        <title>Jinja Sample</title>
    </head>
<body>
    {% include "editorial.html" %} 
</body>
</html>

使用 Flask

启动一个flask应用程序,如下所示:
from flask import Flask, render_template
app = Flask(__name__)


@app.route("/")
def editorial_test():
    editorial_info = {
        "title" : "Hello",
        "image" : "https://images.dog.ceo/breeds/collie-border/n02106166_2595.jpg",
        "subtitle" : "world",
        "article" : "Yeah. But Parasite? It should have been Gone with the Wind!"
    }

    return render_template('template.html', editorial=editorial_info)


if __name__ == '__main__':
    app.run(debug=True)

我没有测试上面的代码。看看这个优秀的tutorial进一步澄清。

直接渲染文件

如果你不想使用 Flask,你可以像这样直接渲染网页(我假设所有文件都在同一个目录中):
import jinja2

editorial_info = {
        "title" : "Hello",
        "image" : "https://images.dog.ceo/breeds/collie-border/n02106166_2595.jpg",
        "subtitle" : "world",
        "article" : "Yeah. But Parasite? It should have been Gone with the Wind!"
    }

templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "template.html"
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render(editorial_info) 

print(outputText)

输出
<html>
    <head>
        <title>Jinja Sample</title>
    </head>
<body>
    <div class="editorial">
    <div class="editorial-title">Hello</div>
    <div class="editorial-image"><img src="https://images.dog.ceo/breeds/collie-border/n02106166_2595.jpg"></div>
    <div class="editorial-subtitle">world</div>
    <div class="editorial-article">Yeah. But Parasite? It should have been Gone with the Wind!</div>
</div>
</body>
</html>

关于python - 是否有更短的方法或 pythonic 方法来生成遵循使用 BeautifulSoup 模式的自定义 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60350625/

相关文章:

python - 子类实例化期间出现 AttributeError

python - 简洁的Python方法来改变一个值,如果它不是Falsey

python pty.fork - 它是如何工作的

python - 离散轴 circos 绘图软件推荐

python - PyQt5 中文本框的 OnClick 事件?

html - 与有序列表对齐

html - 我想让一个 css 样式无效,但不想将该样式设置为任何值

Python用户输入和打印

css - 为什么包含的 div 无法识别其中元素的高度?

python-3.x - 似乎找不到请求头的 requestID 参数的问题