BinWone BinWone - 1 month ago 12
Python Question

How can I get the text use xpath from the text?

I want get

111111111
use
xpath
from the
text
, I do it this way below, but I cannot get it.

import lxml
text = """<p class="tel">
<span class="dsTitle">tel:</span>
111111111
</p>
"""
doc = lxml.etree.fromstring(text, parser=lxml.etree.HTMLParser())
tel = doc.xpath('//p/text')
print(tel)


The output is
[]
.I think I can get
111111111
.
But Why? What's wrong with this?

uL1 uL1
Answer

Use xpath node test text():

It selects all children of type text-node of your context-node p.

Append the filter /text()[normalize-space()] to remove leading and trailing white-space nodes, like new lines and whitespaces.

Complete:

tel = doc.xpath('//p/text()[normalize-space()]')