Python Forum
[ElementTree] Insert big block of HTML? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [ElementTree] Insert big block of HTML? (/thread-37203.html)



[ElementTree] Insert big block of HTML? - Winfried - May-12-2022

Hello,

I have only shallow experience with XML parsers — although I'm currently dealing with HTML.

Unless I missed it, neither lxml nor ElementTree provides a function to insert a big block of HTML at a location in the tree. Before I use a regex instead of those XML parsers… do you confirm?

Thank you.

Here's what it could look like:
import lxml.etree as et
from lxml import html
from io import StringIO

with open("input.html") as reader:
	content = reader.read()
parser = et.HTMLParser(encoding='latin1',remove_blank_text=True,recover=True)
tree = et.parse(StringIO(content), parser)
root = tree.getroot()

body = root.find("body")
with open("block.html") as reader:
	big_block = reader.read()
#HERE
body.insert_after(big_block)