簡體   English   中英

如何從文本中獲取文本使用xpath?

[英]How can I get the text use xpath from the text?

我想從text獲取111111111使用xpath ,我在下面這樣做,但我無法得到它。

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)

輸出是[] 。我想我可以得到111111111 但為什么? 這有什么問題?

使用xpath node test text()

它選擇context-node p text-node類型的所有子節點。

附加filter /text()[normalize-space()]以刪除前導和尾隨空白節點,如新行和空格。

完成:

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

您可以使用xpath如下:

//span[contains(text(),'tel:')]/following-sibling::text()[1]

你忘了括號。

doc.xpath('//p/text()')

將工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM