[英]Get attribute values matching xpath
Consider the following sample HTML:考虑以下示例 HTML:
<html>
<head></head>
<body>
<a href="link1.html">link1</a>
<a href="link2.html">link2</a>
</body>
</html>
$x('/html/body/a/@href')
in the Chrome developer console gives me two results: Chrome 开发者控制台中的$x('/html/body/a/@href')
给了我两个结果:
Now instead of just matching href
, I want to extract the attribute values of href
, so the result I want is an array ["link1.html", "link2.html"]
.现在不只是匹配href
,我想提取href
的属性值,所以我想要的结果是一个数组["link1.html", "link2.html"]
。 How can I do that?我怎样才能做到这一点?
You can't get an attribute's value directly using xPath
, you'll need, after getting the href
, to loop over them to get each value您不能直接使用xPath
获取属性的值,您需要在获取href
后遍历它们以获取每个值
$x('/html/body/a/@href')[0].value
$x('/html/body/a/@href')[1].value
Something like this:像这样的东西:
$x('/html/body/a/@href').map(x => x.value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.