js操作xpath
作者:互联网
function xpath(source, urlXpath) {
const parse = new DOMParser();
const doc = parse.parseFromString(source, 'text/html');
const resultIterator = doc.evaluate(urlXpath, doc, null, XPathResult.ANY_TYPE, null);
let resultNode = resultIterator.iterateNext();
while (resultNode) {
var value = resultNode.textContent
console.log(value)
resultNode = resultIterator.iterateNext();
}
}
var xpathHelper = () => {
var source = document.documentElement.innerHTML
var current_xpath = "//div[@class='wrapper clearfix'][3]/div[@class='content'][1]/div[@class='content-mod'][1]/ul[@class='content-list imglazyload'][1]/li/div[@class='info-mod clearfix'][1]/div[@class='info-head'][1]/a[1]"
xpath(source, current_xpath)
}
xpathHelper()
测试链接
http://news.zol.com.cn/more/2_2000.shtml
标签:xpath,resultNode,js,source,var,操作,div,class 来源: https://www.cnblogs.com/c-x-a/p/13497150.html