guettli
guettli

Reputation: 27969

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

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.

Upvotes: 0

Views: 172

Answers (1)

guettli
guettli

Reputation: 27969

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))

Upvotes: 2

Related Questions