0

When using lxml I often want more information about an element. When using print (element) one does not get much information. Is it possible to change this? It would be nice if I could write a custom function that displays more information, e.g. the element's text. I already tried setattr, but this does not work. The reason for this is explained here.

An example:

from lxml import etree

root = etree.fromstring("<html>this is a test</html>")

print (root)

The is results in: <Element html at 0x7f582daeb700>

Update: Ideally, I would want to be able to just write print (element) and get a nicely formatted output. I was hoping that I could achieve this using a custom __str__ method somehow.

T-Dawg
  • 83
  • 6
  • 2
    Did you try `etree.tostring()`, perhaps with a `pretty_print = True` parameter? – gimix Aug 26 '21 at 10:10
  • My aim is to achieve this using just print(element), e.g. by adding a __str__ method. In the method I could then add etree.tostring() indeed. (Updated the question accordingly to make this clearer) – T-Dawg Aug 26 '21 at 11:09
  • 1
    You _may_ subclass `etree`, of course, but tbh that seems to me far more complicated than just using `print(element.tostring())` – gimix Aug 26 '21 at 12:28
  • Subclassing probably would not help, as the etree functions would still return "normal" etree-Elements. I guess what I am looking for is simply not possible. – T-Dawg Aug 28 '21 at 10:32

2 Answers2

1

You can get the element text by print(root.text)

  • Thanks for the answer! My hope is to just use print(element) without an external function, so this will not work for me. I updated the question accordingly. – T-Dawg Aug 26 '21 at 11:14
0

You can do it without any external library. Just like the below:

import xml.etree.ElementTree as ET

root = ET.fromstring("<html>this is a test</html>")
ET.dump(root)

output

<html>this is a test</html>
balderman
  • 22,927
  • 7
  • 34
  • 52