<?xml version="1.0" encoding="UTF-8"?><div id="app" class="grid bg-local font-body justify-center" style="background-image: url("/img/picture.jpg");"> <div data-v-f7g8b83d=" " data-fruit-code="**I WANT TO GET WHAT'S IN HERE**" class="note relative bg-background items-center select-none w-56 sm:w-64 pb-4" style="transform: rotate(6deg);"/> <div data-v-f7g8b83d=" " data-fruit-code="**I WANT TO GET WHAT'S IN HERE 1**" class="note relative bg-background items-center select-none w-56 sm:w-64 pb-4" style="transform: rotate(6deg);"/> <div data-v-f7g8b83d=" " data-fruit-code="**I WANT TO GET WHAT'S IN HERE 2**" class="note relative bg-background items-center select-none w-56 sm:w-64 pb-4" style="transform: rotate(6deg);"/> <div data-v-f7g8b83d=" " data-fruit-code="**I WANT TO GET WHAT'S IN HERE 3**" class="note relative bg-background items-center select-none w-56 sm:w-64 pb-4" style="transform: rotate(6deg);"/> <div data-v-f7g8b83d=" " data-fruit-code="**I WANT TO GET WHAT'S IN HERE 4**" class="note relative bg-background items-center select-none w-56 sm:w-64 pb-4" style="transform: rotate(6deg);"/> </div>我正在嘗試構(gòu)建一個(gè)機(jī)器人來抓取特定網(wǎng)站。我希望能夠獲取與“data-fruit-code”相關(guān)的文本。我想出了這個(gè)://*[@id="app"]/div[2]/div和這個(gè)://*[@data-fruit-code]然而,兩者都只突出顯示整個(gè) div。我覺得我在這里錯(cuò)過了一些東西。我可以添加什么或者如何修復(fù)現(xiàn)有的 XPATH 命令,以便它只獲取“data-fruit-code”文本?我嘗試添加 text() 和 word() 但這些對我來說也不起作用。以下是我用來獲取幫助的一些參考資料。https://devhints.io/xpath#class-checkhttps://developer.mozilla.org/en-US/docs/Web/XPath
2 回答

MM們
TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
請注意,這data-fruit-code
稱為屬性,并在 XPath 中通過其名稱前面的前綴進(jìn)行選擇@
。
有很多方法可以選擇目標(biāo)屬性。這里有兩個(gè)有趣的可能性:
這個(gè) XPath,
//@data-fruit-code
data-fruit-code
將選擇文檔中的所有屬性。這個(gè) XPath,
//div[@id="app"]/div/@data-fruit-code
將選擇其父元素具有 且屬性值為 的元素上的所有
data-fruit-code
屬性。div
div
id
app

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試
//div[@data-fruit-code]/@data-fruit-code
輸出
**I WANT TO GET WHAT'S IN HERE**
**I WANT TO GET WHAT'S IN HERE 1**
**I WANT TO GET WHAT'S IN HERE 2**
**I WANT TO GET WHAT'S IN HERE 3**
**I WANT TO GET WHAT'S IN HERE 4**
- 2 回答
- 0 關(guān)注
- 273 瀏覽
添加回答
舉報(bào)
0/150
提交
取消