<?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>我正在尝试构建一个机器人来抓取特定网站。我希望能够获取与“data-fruit-code”相关的文本。我想出了这个://*[@id="app"]/div[2]/div和这个://*[@data-fruit-code]然而,两者都只突出显示整个 div。我觉得我在这里错过了一些东西。我可以添加什么或者如何修复现有的 XPATH 命令,以便它只获取“data-fruit-code”文本?我尝试添加 text() 和 word() 但这些对我来说也不起作用。以下是我用来获取帮助的一些参考资料。https://devhints.io/xpath#class-checkhttps://developer.mozilla.org/en-US/docs/Web/XPath
2 回答
MM们
TA贡献1886条经验 获得超2个赞
请注意,这data-fruit-code
称为属性,并在 XPath 中通过其名称前面的前缀进行选择@
。
有很多方法可以选择目标属性。这里有两个有趣的可能性:
这个 XPath,
//@data-fruit-code
data-fruit-code
将选择文档中的所有属性。这个 XPath,
//div[@id="app"]/div/@data-fruit-code
将选择其父元素具有 且属性值为 的元素上的所有
data-fruit-code
属性。div
div
id
app
拉丁的传说
TA贡献1789条经验 获得超8个赞
尝试
//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 关注
- 173 浏览
添加回答
举报
0/150
提交
取消