![](/img/trans.png)
[英]Finding multiple types of tags with lxml findall() with xpath?
[英]Findall equivalent for xpath , Lxml
我正在提取有關標簽的文本,我需要以wrt p標簽的列表形式獲取它們。 我有這個xpath表達式為:
find = etree.XPath("//w:p//.//*[local-name() = 'ins']//text()" ,namespaces={'w':"http://schemas.openxmlformats.org/wordprocessingml/2006/main"})
我想在findall
表達式中使用它。 我試過了:
inserted_list_1=[]
for p in lxml_tree.findall('.//{' + w + '}p'):
inserted_list_1.append([t.text for t in p.findall('.//{' + w + '}ins')])
但是所有返回的結果都是一個None
值的列表,而以前的xpath可以完美運行。
我認為缺少一些中間路徑。
您不能將該表達式與findall()
; findall()
方法故意與有限的ElementTree API XPath支持保持兼容性。
使用xpath()
方法代替:
for p in lxml_tree.xpath('.//w:p', namespaces={'w': w}):
並僅使用名稱空間前綴進行可讀性更高的查詢。
如果您只想提取所有包含的文本,則可以使用:
[t for t in p.xpath('../w:p//w:ins//text()',namespaces={'w': w})]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.