用nodejs吧 开个端口调用
npm install jsdom安装一下jsdom[JavaScript] 纯文本查看 复制代码 var htmlCode = '<li class="pdf-dir"><i class="fa fa-folder-open"></i><span>2021年款吉利博瑞ePRO</span><ul style=""><li class="pdf-dir"><i class="fa fa-folder-open"></i><span>01 维修手册</span><ul style=""><li class="pdf-dir"><i class="fa fa-folder-open"></i><span>01 车型概述</span><ul style=""><li class="pdf-dir"><i class="fa fa-folder-open"></i><span>举升车辆</span><ul style=""><li class="pdf-end hasview" exetn=".pdf"><i class="fa fa-file-pdf-o"></i><span>提升和举升车辆</span></li></ul></li><li class="pdf-dir"><i class="fa fa-folder-open"></i><span>健康和安全</span><ul style=""><li class="pdf-end hasview viptip" exetn=".pdf"><i class="fa fa-file-pdf-o"></i><span>一般车间工具与设备</span></li><li class="pdf-end hasview viptip" exetn=".pdf"><i class="fa fa-file-pdf-o"></i><span>制动液</span></li><li class="pdf-end hasview viptip" exetn=".pdf"><i class="fa fa-file-pdf-o"></i><span>化学材料</span></li><li class="pdf-end hasview viptip" exetn=".pdf"><i class="fa fa-file-pdf-o"></i><span>发动机冷却液</span></li><li class="pdf-end hasview viptip" exetn=".pdf"><i class="fa fa-file-pdf-o"></i><span>噪音</span></li>';
const { JSDOM } = require('jsdom');
const htmlDoc = new JSDOM(htmlCode).window.document;
//const parser = new DOMParser();
//const htmlDoc = parser.parseFromString(htmlCode, "text/html");
const ulElement = htmlDoc.querySelector('li.pdf-dir > ul');
const jsonFormatted = {[htmlDoc.querySelector('li.pdf-dir > span').textContent]: {}};
function getChildNodes(ulElement, jsonObj) {
Array.from(ulElement.children).forEach((childElement) => {
const childKey = childElement.querySelector('span')?.textContent;
if (childKey) {
if (childElement.querySelector('ul')) {
jsonObj[childKey] = {};
getChildNodes(childElement.querySelector('ul'), jsonObj[childKey]);
} else {
jsonObj[childKey] = [];
}
}
});
}
getChildNodes(ulElement, jsonFormatted[[htmlDoc.querySelector('li.pdf-dir > span').textContent]]);
var aaa=JSON.stringify(jsonFormatted, null, 2);
console.log(aaa)
https://github.com/jsdom/jsdom |