借助浏览器的xpath功能提取word的文本数据,实现word数据转换成json格式
实现功能
1. 数据样式

2. 解析结果

实现方式
1. word数据导入到雀语中
2. 使用浏览器的xpath获取数据,数据打印控制台

function wordParser(){
// 上下文
let context={
answers:{},// 存放问题、答案键值对
currentQuestion:''// 当前问题
};
// 按行解析
let parse=()=>{
let parser=parseQuestion;
$x('//ne-p').forEach((line)=>{
parser=parser(context,line);
})
console.log(context);
};
// 解析问题
let parseQuestion=(context,line)=>{
if(line.innerText.trim().length<=0){
return parseQuestion;
}
context.answers[line.innerText.trim().replaceAll('"','')]=[];
context.currentQuestion=line.innerText.trim().replaceAll('"','');
return parseAnswer;
};
// 解析问题的答案
let parseAnswer=(context,line)=>{
if(line.getElementsByTagName('ne-text')[0].innerText.trim().length==0){// 解析新的问题
return parseQuestion;
}
let texts=line.getElementsByTagName('ne-text');
if(texts[0].style.color==='red'){
context.answers[context.currentQuestion].push(line.innerText.substring(0,line.innerText.length-1).substring(2,line.innerText.length).replaceAll('"',''));
}
return parseAnswer;// 可能有多个答案
}
return {context,parse};
}
let result=wordParser();
result.parse();
console.log(result.context.answers);
3. 复制提取结果

网友评论