gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:49:50 25 4
gpt4 key购买 nike

我正在构建 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com