简体   繁体   English

如何使用 XPath 获取 Image src 属性值

[英]How to get Image src attribute value using XPath

I want to extract the image url from the tag using XPath but the only problem is it returning whole src attr using the below XPath.我想使用 XPath 从标签中提取图像 url 但唯一的问题是它使用下面的 XPath 返回整个 src attr。

Query used in Chrome inspect tool: Chrome 检查工具中使用的查询:

$x("//div[@class='specs-photo-main']//img/@src")

Output: Output:

[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)

Is there is a way to get the "nodeValue" of "src" attribute?有没有办法获取“src”属性的“nodeValue”?

You need to access the first element (as you need to get the first node found) and then you can get the properties of the found node:您需要访问第一个元素(因为您需要找到找到的第一个节点),然后您可以获得找到的节点的属性:

$x("//div[@class='specs-photo-main']//img/@src")[0].nodeValue

Or,或者,

$x("//div[@class='specs-photo-main']//img/@src")[0].textContent

if you need to get the plain text value.如果您需要获取纯文本值。

See also nodeValue vs innerHTML and textContent.另请参阅nodeValue 与 innerHTML 和 textContent。 How to choose?如何选择? and innerText vs innerHTML vs label vs text vs textContent vs outerText .innerText vs innerHTML vs label vs text vs textContent vs outerText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM