【发布时间】:2021-07-09 08:29:42
【问题描述】:
如何用html5lib替换所有标签的innerHTML?
输入:
foo
<h1>Moonlight</h1>
bar
期望的输出:
foo
<h1>Sunshine</h1>
bar
我想使用 html5lib,因为它已经是一个依赖项了。
【问题讨论】:
标签: python html-parsing html5lib
如何用html5lib替换所有标签的innerHTML?
输入:
foo
<h1>Moonlight</h1>
bar
期望的输出:
foo
<h1>Sunshine</h1>
bar
我想使用 html5lib,因为它已经是一个依赖项了。
【问题讨论】:
标签: python html-parsing html5lib
from xml.etree import ElementTree
from html5lib import HTMLParser
parser = HTMLParser(namespaceHTMLElements=False)
tree = parser.parse('''
foo
<h1>Moonlight</h1>
bar''')
for e in tree.findall('.//h1'):
e.text = 'Sunshine'
print(ElementTree.tostring(etree))
【讨论】: