這裏Xpath的蟒蛇發現節點是HTML代碼:後特定的文本
<div id="someid">
<h2>Specific text 1</h2>
<a class="hyperlinks" href="link"> link1 inside specific text 1</a>
<a class="hyperlinks" href="link"> link2 inside specific text 1</a>
<a class="hyperlinks" href="link"> link3 inside specific text 1</a>
<h2>Specific text 2</h2>
<a class="hyperlinks" href="link"> link1 inside specific text 2</a>
<a class="hyperlinks" href="link"> link2 inside specific text 2</a>
<a class="hyperlinks" href="link"> link3 inside specific text 2</a>
<a class="hyperlinks" href="link"> link4 inside specific text 2</a>
<h2>Specific text 3</h2>
<a class="hyperlinks" href="link"> link1 inside specific text 3</a>
<a class="hyperlinks" href="link"> link2 inside specific text 3</a>
</div>
我必須清楚地找到下的每個「特定文字」鏈接。問題是,如果我用Python語言編寫如下代碼:
links = root.xpath("//div[@id='someid']//a")
for link in links:
print link.attrib['href']
它打印出的所有環節,不論「特定文字X」,而我想是這樣的:
print "link under Specific text:"+specific+" link:"+link.attrib['href']
請建議
那麼,基於提供的XML文檔,您想要的確切輸出是什麼?這不清楚。請編輯您的問題並添加此要求。 –