简体   繁体   English

与xpath,Lxml等效的Findall

[英]Findall equivalent for xpath , Lxml

I am extracting text with respect to tags and I need to get them in a list form wrt p tags. 我正在提取有关标签的文本,我需要以wrt p标签的列表形式获取它们。 I have this xpath expression as: 我有这个xpath表达式为:

 find =  etree.XPath("//w:p//.//*[local-name() = 'ins']//text()" ,namespaces={'w':"http://schemas.openxmlformats.org/wordprocessingml/2006/main"}) 

And i want to use it in a findall expression. 我想在findall表达式中使用它。 I tried: 我试过了:

inserted_list_1=[]
for p in lxml_tree.findall('.//{' + w + '}p'):
    inserted_list_1.append([t.text for t in p.findall('.//{' + w + '}ins')])

but all this returns is a list full of None values whilst the former xpath works perfectly. 但是所有返回的结果都是一个None值的列表,而以前的xpath可以完美运行。
I think there's some intermediate path missing. 我认为缺少一些中间路径。

You cannot use that expression with findall() ; 您不能将该表达式与findall() the findall() method deliberately keeps compatibility with the limited ElementTree API XPath support . findall()方法故意与有限的ElementTree API XPath支持保持兼容性。

Use the xpath() method instead: 使用xpath()方法代替:

for p in lxml_tree.xpath('.//w:p', namespaces={'w': w}):

and just use namespace prefixes for much more readable queries. 并仅使用名称空间前缀进行可读性更高的查询。

If you just wanted to extract all contained text, you can use: 如果您只想提取所有包含的文本,则可以使用:

[t for t in p.xpath('../w:p//w:ins//text()',namespaces={'w': w})]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM