Python Forum
[ElementTree] Insert big block of HTML?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[ElementTree] Insert big block of HTML?
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 1,072 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,746 May-27-2022, 04:59 PM
Last Post: Winfried
  ElementTree get attribute value part of string paulo79 1 2,358 Apr-05-2022, 09:13 PM
Last Post: deanhystad
  reading html and edit chekcbox to html jacklee26 5 3,278 Jul-01-2021, 10:31 AM
Last Post: snippsat
  xml.etree.ElementTree question. water 0 3,414 Oct-09-2020, 06:47 PM
Last Post: water
  xml.etree.ElementTree extract string values matthias100 2 5,302 Jul-12-2020, 06:02 PM
Last Post: snippsat
  Python 3 Elementtree and Comment First gw1500se 3 3,986 May-25-2020, 09:02 PM
Last Post: gw1500se
  Write the XML file from elementtree with hexa decimal encoding Dillibabu 4 3,631 Dec-24-2019, 10:10 AM
Last Post: Dillibabu
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 2,040 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  Please help me error "AttributeError: 'module' object has no attribute 'ElementTree'" mattroi261192 4 7,240 Nov-11-2018, 05:01 AM
Last Post: mattroi261192

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020