jar*_*jar 5 html python beautifulsoup python-3.x
我正在构建 HTML 作为一个更大项目的一部分。建筑工程,没有问题。但是我担心代码太冗长或者我没有使用 BeautifulSoup 的全部功能。
例如:我正在生成一个divclass 标签,它按顺序editorial包装了一个divclass 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>
Run Code Online (Sandbox Code Playgroud)
这是适用于我正在尝试做的小型演示版本的长代码 -
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())
Run Code Online (Sandbox Code Playgroud)
它可以完成工作,但我觉得它太长了。有没有更优雅的方法来实现这一目标?
对于您正在执行的任务,我强烈考虑使用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>
Run Code Online (Sandbox Code Playgroud)
将其包含editorial.html在以下 html 文件中,该文件将由 Flask 加载。在此示例中,这将用作您的基本模板。
<!-- template.html -->
<html>
<head>
<title>Jinja Sample</title>
</head>
<body>
{% include "editorial.html" %}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
使用烧瓶
启动一个 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)
Run Code Online (Sandbox Code Playgroud)
我没有测试上面的代码。看看这个优秀的教程以获得进一步的说明。
直接渲染文件
如果你不想使用 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)
Run Code Online (Sandbox Code Playgroud)
输出
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
137 次 |
| 最近记录: |