[英]Get more values from XPATH in Javascript
I have this HTML template: 我有这个HTML模板:
<div class="item">
<span class="light">Date</span>
<a class="link" href="">2018</a>
(4pop)
</div>
<div class="item">
<span class="light">From</span>
<span>
<a class="link" href="" title="Bob" itemprop="url"><span itemprop="name">Bob</span></a>,
</span>
<span>
<a class="link" href="" title="Peter" itemprop="url"><span itemprop="name">Peter</span></a>
</span>
</div>
<div class="item">
<span class="light">For</span>
<a class="link" href="">Bak</a>,
<a class="link" href="">Cam</a>,
<a class="link" href="">Oli</a>
</div>
<div class="item">
<span class="light">Nat</span>
<a class="link" href="">Cool</a>
</div>
</div>
And my Javascript code: 和我的Javascript代码:
var doc = new DOMParser().parseFromString(HTMLContent,'text/html');
var infos = doc.evaluate('//div[@class="item"]/span[1]', doc, null, XPathResult.ANY_TYPE, null);
var nodes = [];
for(var node = infos.iterateNext(); node; node = infos.iterateNext()) {
nodes.push(node);
console.log(node.textContent);
// Until here, all things works well ! Except the code from here:
var nodde = node.nextElementSibling.attributes;
nodde.forEach(function(item){
console.log(item);
});
}
My goal is to get the respective value for each categorie, for example: 我的目标是获取每个类别的相应值,例如:
Date = 2018, (4pop)
From = Bob, Peter
For = Bak, Cam, Oli
Nat = Cool
I tried to iterate: node.nextElementSibling.attributes
but without any success ! 我试图进行迭代:
node.nextElementSibling.attributes
但没有成功!
Is there a way to get the expected result please ? 请问有什么方法可以得到预期的结果吗?
The HTML Document Object Model, DOM, is preferred as it's easier. 首选HTML文档对象模型DOM,因为它更容易。 XPATH is geared toward more rigid XML documents.
XPATH面向更严格的XML文档。
Using JavaScript, you would get data with something like: 使用JavaScript,您将获得类似以下内容的数据:
var users = document.querySelectorAll('[itemprop=name]').textContent;
console.log(users);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.