各位大大好
小弟最近在幫公司做一個網頁的解析程式,需要透過 Xpath 做網頁元素的篩選,我的目標網站架構大致如下:
<div class='my_target' id='A'>
This is a sample website HTML from A!
<span>APPLE</span>
<span>BANANA</span>
<span>ORANGE</span>
<span>IGNORE_1</span>
<span>IGNORE_2</span>
</div>
<div class='not_my_target' id='B'>
This is a sample website HTML from B!
<span>APPLE</span>
<span>BANANA</span>
<span>ORANGE</span>
<span>IGNORE_1</span>
<span>IGNORE_2</span>
</div>
而我希望可以過 Xpath 選到 div A 的前三個元素,預期結果如下:
This is a sample website HTML from A!
APPLE
BANANA
ORANGE
原本參考文件後的寫法如下,不過都沒辦法正常選出我要的元素,因為對 Xpath 還沒有很熟悉,不知道這樣寫有沒有甚麼問題 @@
//div[@id='A' and (not(self::span and contains(text(), "IGNORE_1")) or not(self::span and contains(text(), "IGNORE_2"))]
麻煩各位大大協助小弟了,感恩感恩!
stackoverflow大神的解法...
https://stackoverflow.com/questions/4759746/xpath-get-first-10-items-of-selected-set
FYI