iT邦幫忙

0

Xpath 的 Exclude 功能 (not / except) 運用疑問求助

各位大大好

小弟最近在幫公司做一個網頁的解析程式,需要透過 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"))]

麻煩各位大大協助小弟了,感恩感恩!
/images/emoticon/emoticon02.gif

froce iT邦大師 1 級 ‧ 2021-07-16 10:14:01 檢舉
1. 用什麼工具爬?
2. 幹麻不用css selector?
huahualiu iT邦新手 2 級 ‧ 2021-07-16 11:11:59 檢舉
@froce
系統是用 selenium 爬
而既有系統 就是使用 Xpath 做element 選取
系統會針對選取元素做截圖
現在是因為要加 feature @@

大改會出事XD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

我要發表回答

立即登入回答