我想使用XPath从标记中提取图像url,但唯一的问题是它使用下面的XPath返回整个src。
用于Chrome检查工具的查询:
$x("//div[@class='specs-photo-main']//img/@src")输出:
[src]
0: src
baseURI: "https://www.gsmarena.com/xiaomi_11i_hypercharge-11186.php"
childNodes: NodeList []
firstChild: null
isConnected: false
lastChild: null
localName: "src"
name: "src"
namespaceURI: null
nextSibling: null
nodeName: "src"
nodeType: 2
nodeValue: "https://fdn2.gsmarena.com/vv/bigpic/xiaomi-11i-hypercharge.jpg"
ownerDocument: document
ownerElement: img
parentElement: null
parentNode: null
prefix: null
previousSibling: null
specified: true
textContent: "https://fdn2.gsmarena.com/vv/bigpic/xiaomi-11i-hypercharge.jpg"
value: "https://fdn2.gsmarena.com/vv/bigpic/xiaomi-11i-hypercharge.jpg"
[[Prototype]]: Attr
length: 1
[[Prototype]]: Array(0)有办法获取"src“属性的"nodeValue”吗?
发布于 2022-01-23 19:10:41
您需要访问第一个元素(就像您需要找到第一个节点一样),然后您可以获得找到节点的属性:
$x("//div[@class='specs-photo-main']//img/@src")[0].nodeValue或,
$x("//div[@class='specs-photo-main']//img/@src")[0].textContent如果您需要获得纯文本值。
另见nodeValue vs innerHTML and textContent. How to choose?和innerText vs innerHTML vs label vs text vs textContent vs outerText。
https://stackoverflow.com/questions/70819687
复制相似问题