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,但沒有具體。任何線索?
+1那工作.. – jerrymouse 2012-03-28 10:12:42