【发布时间】:2021-12-03 18:32:59
【问题描述】:
有什么方法可以控制展开的深度吗?我的 HTML 有时包含 css。并且美化为每个标签添加换行符...
<html><body><h1>hello world</h1></body></html>
到:
<html>
<body><h1>hello world</h1></body>
</html>
from bs4 import BeautifulSoup
INPUT_FILE = "html_unformatted.txt"
OUTPUT_FILE = "index.html"
unicode_data = open(INPUT_FILE, "r", encoding='unicode_escape').read()
data = unicode_data.encode('iso-8859-1').decode('utf-8')
soup = BeautifulSoup(data, features="html.parser")
pretty_html = soup.prettify()
with open(OUTPUT_FILE, "w") as f:
f.write(pretty_html)
print(f"Wrote to {OUTPUT_FILE}")
我有:
<html>
<body>
<h1>
hello world
</h1>
</body>
</html>
【问题讨论】:
标签: python beautifulsoup