gpt4 book ai didi

python - 缩短 HTML 文件

转载 作者:行者123 更新时间:2023-12-05 02:23:13 26 4
gpt4 key购买 nike

是否有缩短 HTML 页面的库(最好是 Python 库)?我的意思是它会生成一个可能更小的(就字符数而言,包括换行符 <- 想想字符串的长度)呈现的与原始页面完全相同的 HTML 页面?

例如:

<b>
Silly example
</b>

可以改成:

<b>Silly example</b>

最终结果是一样的:

愚蠢的例子

最佳答案

您可以使用 BeautifulSoup在 Python 中美化(而不是缩小)HTML 或 XML 代码。

from bs4 import BeautifulSoup
soup = BeautifulSoup('file.html')
prettified = soup.prettify(encoding="utf8")

要在 Python 中缩小 HTML,您可以使用 htmlmin . htmlmin.minify 的更多参数可以在 documentation 中找到.

import htmlmin

with open('file.html', 'r') as f:
content = f.read()
minified = htmlmin.minify(content, remove_empty_space=True)

关于python - 缩短 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24435774/

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