仅解析必要的html数据,并跳过不需要的html数据

时间:2018-10-15 18:14:04

标签: javascript html json xmlhttprequest html-parsing

我正在处理一个获取URL并解析其所有html的脚本。但是它仅使用“数据样式名称”,“ href”,“数据售罄”和“选择”。

这是我解析html的方式:

function loadHTMLSource(urlSource) {
    xhttp = new XMLHttpRequest();
    xhttp.open("GET", urlSource, false);
    xhttp.send();
    return xhttp.response;
}

var page_html = loadHTMLSource(url); 

parser = new DOMParser();

my_document = parser.parseFromString(page_html, "text/html");

我只是从这些html内容中获取信息

my_document.querySelectorAll("[data-style-name]");

attributes["data-sold-out"].value

my_document.querySelector("meta[name='csrf-token']");

my_document.querySelector('select');

是否可以仅提取这些特定的html内容。所以我最终不会提取不需要的数据吗?

感谢您的帮助。谢谢。

0 个答案:

没有答案