2012-03-28 139 views
0

我必須收集所有類別名稱及其下的所有div,並以'config-entry'開始。選擇兩個節點之間的兄弟節點

<h2>category 1</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<h2>category 2</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<h2>category 3</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<h2>category 4</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 

我使用XPath //h2[1]/following-sibling::h2[1]/preceding-sibling::div[starts-with(@class,'config-entry')]這樣的:

categories = root.xpath("//h2") 
for i in xrange(len(categories)): 
    print "----%s----" % categories[i].text 
    contents = root.xpath("//h2[1]/following-sibling::h2[1]/preceding-sibling::div[starts-with(@class,'config-entry')]") 
    print len(contents) 

此代碼僅適用於1類作品好選擇第1類和2之間的所有div但砸了以後。我玩過h2[1],將它改爲0,2,3,但沒有具體。任何線索?

回答

2

我建議使用h2標籤和div標籤,這將返回他們在文檔順序,然後當你處理它們,每個格「屬於」的工會最後h2你所看到的。

E.g.

'//h2|//div[contains(@class,"config-entry")]' 

工作例如:

from lxml import etree 

doc = etree.HTML(""" 
<html> 
<h2>category 1</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<h2>category 2</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<h2>category 3</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<h2>category 4</h2> 
<div class='clear10'></div> 
<div class='config-entry selected-block'>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
<div class='config-entry '>...</div> 
</html>""") 

category = None 
for ele in doc.xpath('//h2|//div[contains(@class,"config-entry")]'): 
    if ele.tag == 'h2': 
    category = str(ele.text) 
    else: 
    if category: 
     print "%s: %s, %r" % (category,ele.tag,ele.attrib) 

產量:

category 1: div, {'class': 'config-entry selected-block'} 
category 1: div, {'class': 'config-entry '} 
category 1: div, {'class': 'config-entry '} 
category 1: div, {'class': 'config-entry '} 
category 2: div, {'class': 'config-entry selected-block'} 
category 2: div, {'class': 'config-entry '} 
category 2: div, {'class': 'config-entry '} 
category 2: div, {'class': 'config-entry '} 
category 2: div, {'class': 'config-entry '} 
category 3: div, {'class': 'config-entry selected-block'} 
category 3: div, {'class': 'config-entry '} 
category 4: div, {'class': 'config-entry selected-block'} 
category 4: div, {'class': 'config-entry '} 
category 4: div, {'class': 'config-entry '} 
category 4: div, {'class': 'config-entry '} 
+0

+1那工作.. – jerrymouse 2012-03-28 10:12:42

相關問題