2016-01-20 72 views
0

我使用LXML刪除一些UL標籤,就像這樣:LXML的HtmlElement drop_tree失敗

for ele in doc.iter('ul'): 
    if len(ele.findall('.//a'))/len(ele.findall('.//li')) >= 2: 
     ele.drop_tree() 

但這不工作,不刪除任何UL標記。

如果我這樣做:

for ele in doc.iter('ul'): 
    ele.drop_tree() 

所有UL標籤去掉。 任何人都可以幫助我,謝謝。

回答

0

「LEN(ele.findall( './/禮'))」 的值MAB是零,你可以嘗試:

html = lxml.html.fragment_fromstring("<root><child>Child 1</child><child>Child 2</child><another>Child 3</another><another>Child 4</another></root>") 
for elem in html.iter('another'): 
    for sub in elem.findall('.'): 
     if 'Child 4' in sub.itertext(): 
      html.remove(sub) 
print(lxml.html.tostring(html, encoding='unicode')) 
+0

感謝。但是如果我加一個len(ele.findall('.// li')),結果不會被改變。 – Wallace