Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用lxml仅检索可见节点文本_Python_Lxml - Fatal编程技术网

Python 如何使用lxml仅检索可见节点文本

Python 如何使用lxml仅检索可见节点文本,python,lxml,Python,Lxml,如何使用Python包检索节点中的可见文本(不包括任何子节点或隐藏元素) 我在文档中只能找到node.text\u content(),但所做的只是去掉html标记,返回所有深度的所有文本,而不管可见性如何。我还尝试了node.text,但似乎对所有节点都不返回任何值。您可以使用xpath提取所需的文本。 因为您只需要可见文本,所以将/text()放在xpath中 例如,如果我需要摘录你的第二段: import requests from lxml import etree r = reques

如何使用Python包检索节点中的可见文本(不包括任何子节点或隐藏元素)


我在文档中只能找到node.text\u content(),但所做的只是去掉html标记,返回所有深度的所有文本,而不管可见性如何。我还尝试了
node.text
,但似乎对所有节点都不返回任何值。

您可以使用xpath提取所需的文本。 因为您只需要可见文本,所以将
/text()
放在xpath中

例如,如果我需要摘录你的第二段:

import requests
from lxml import etree
r = requests.get("https://stackoverflow.com/questions/32029681/how-to-retrieve-only-visible-node-text-with-lxml")
html = etree.HTML(r.text)
list_of_text = html.xpath('//*[@id="question"]/div[2]/div[2]/div[1]/p//text()') #xpath copied from browser
''.join(list_of_text)
您将获得:

'How do you use the Python package lxml to retrieve visible text in a node, excluding any child nodes or hidden elements?All I can find in the docs is node.text_content(), but all that does is strip out html tags, returning all text at all depths regardless of visibility. I also tried node.text, but that seems to just return None for all nodes.'

请注意,
//text()
/text()
稍有不同,它将选择父节点到最后一个子节点之间的任何节点。

您所说的“可见性”是什么意思?CSS?那不是
lxml
的域,
lxml
只是一个XML/(X)HTML解析器stackower@LukasGraf当然是。它包括cssselect和xpath支持。CSS不是魔法。只是文字。我应该能够在所有深度和不同类型的类和样式下搜索文本…但是他们的文档只告诉你如何找到全部或全部。但我很乐意找到一种在特定节点上查找文本的方法。@Cerin不,事实并非如此。XPath与CSS无关。cssselect只是CSS规范的一小部分,只是选择器——这只是一个将CSS选择器转换为XPath的方便模块。拥有一个成熟的CSS解析器甚至不足以确定节点可见性。您需要一个实际的CSS布局引擎,该引擎考虑继承和选择器的特殊性来确定节点的计算样式。至于元素文本,问题非常清楚:“两个属性
.text
.tail
足以表示XML文档中的任何文本内容。”。因此,为了从节点中提取平面(非递归)文本内容,类似于
u'。join([node.text]+[c.tail for c in node.iterchildren()])
就足够了。