How to replace the innerHTML of all <h1> tags with html5lib?

159 views Asked by At

How to replace the innerHTML of all tags with html5lib?

input:

foo
<h1>Moonlight</h1>
bar

Desired output:

foo
<h1>Sunshine</h1>
bar

I would like to use html5lib, since it is already a dependency.

1

There are 1 answers

0
guettli On
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))