Mike Pennington
Mike Pennington

Reputation: 43077

Parsing lxml.etree._Element contents

I have the following element that I parsed out of a <table>

<td align="center" valign="top">
  <a href="ConfigGroups.aspx?cfgID=451161&amp;prjID=11778&amp;grpID=DTST" 
    target="_blank">
    5548U
  </a><br/>Power La Vaca<br/>(M8025K)<br/>Linux 4.2.x.x<br/>
</td>

I am trying to extract "55488 Power La Vaca (8025K) Linux 4.2.x.x" from this element (including the spaces).

import lxml.etree as ET
td_html = """
<td align="center" valign="top">
  <a href="ConfigGroups.aspx?cfgID=451161&amp;prjID=11778&amp;grpID=DTST" 
    target="_blank">
    5548U
  </a><br/>Power La Vaca<br/>(M8025K)<br/>Linux 4.2.x.x<br/>
</td>
"""

td_elem = ET.fromstring(td_html)

fail_1 = td_elem.find('a').text + td_elem.text
print "FAIL_1", fail_1

print "FAIL_2"
for elem in td_elem.iterchildren():
    print elem.tag, elem.text

Results

$ python textxml.py

FAIL_1
    5548U


FAIL_2
a
    5548U

br None
br None
br None
br None
$

Question

It is humbling that I have to ask this question, since it doesn't seem like it should be hard.

How can I extract "Power La Vaca (8025K) Linux 4.2.x.x" from the td_elem element (including the spaces)?

Please, no regexp solutions.

Solution

The explicit solution (using Finn's suggestion of itertext()):

import lxml.etree as ET
td_html = """
<td align="center" valign="top">
  <a href="ConfigGroups.aspx?cfgID=451161&amp;prjID=11778&amp;grpID=DTST" 
    target="_blank">
    5548U
  </a><br/>Power La Vaca<br/>(M8025K)<br/>Linux 4.2.x.x<br/>
</td>
"""

td_elem = ET.fromstring(td_html)
print "SUCCESS", ' '.join([txt.strip() for txt in td_elem.itertext()])

Upvotes: 4

Views: 5635

Answers (2)

Zach Young
Zach Young

Reputation: 11188

When working with XML, even in Python, I like to try and use the domain specific tools that are available. For parsing bits of XML, XPath is it for me.

>>> td_elem = ET.fromstring(td_html)
>>>
>>> # Use XPath to grab just the text nodes under <td/>, 
>>> # ignoring any text nodes in child nodes of <td/> (i.e., <a...>5548U</a>)
>>> print(td_elem.xpath('/td/text()'))
['\n  ', 'Power La Vaca', '(M8025K)', 'Linux 4.2.x.x', '\n']
>>>
>>> # Make it a little cleaner
>>> ' '.join(x.strip() for x in td_elem.xpath('/td/text()'))
' Power La Vaca (M8025K) Linux 4.2.x.x '
>>>
>>> # Just for reference, grab all text nodes with '//'
>>> ' '.join(x.strip() for x in td_elem.xpath('/td//text()'))
' 5548U Power La Vaca (M8025K) Linux 4.2.x.x '

Upvotes: 3

Finn
Finn

Reputation: 1873

I know there must be a better way but this works.

link = td_elem.find('a').text.strip()
text = ''.join(td_elem.itertext()).strip()
text.split(link)[1]

Output is Power La Vaca(M8025K)Linux 4.2.x.x

Update: This is actually better if you want spaces in place of those <br>s

' '.join(map(str, [el.tail for el in td_elem.iterchildren() if el.tail]))

The map str isn't actually needed for this but I can imagine other values for which it would be.

Upvotes: 3

Related Questions