使用cheerio 将以下代码转换为json <html><head>123</head><body><table><tbody> <!-- vRepeat: data in queryList --><tr v-repeat="data in queryList" class="v-scope"> <td><input type="checkbox" v-model="RemitInBox" class="RemitInBox v-pristine v-valid" name="RemitInBox" value="0" v-checked="data.CheckFlag" v-disabled="data.FndDrc!=\'01\' || data.BusiState!=\'10\'" id="List0_RemitIn" v-click="clickBox($index,data)"></td> <td><span v-show="data.FndDrc!=\'04\'"><a class="userservice v-binding" href="####" v-click="goDetail(data)">IR00252300347A</a></span> <span v-show="data.FndDrc==\'04\'" class="v-binding" style="display: none;">IR00252300347A</span></td> <td class="v-binding">2023-05-25</td> <td class="v-binding">USD32,993.50</td> <td class="v-binding">1/FLOKSER KIMYA SANAYI VE TICARET 1...</td> <td class="v-binding">31980005000107125</td> <td><span v-show="data.FndDrc==\'01\'">入账</span> <span v-show="data.FndDrc==\'04\'" style="display: none;">退汇</span> <span v-show="!(data.FndDrc==\'04\'||data.FndDrc==\'01\')" style="display: none;">待客户确认</span> </td> <td class="v-binding">受理成功</td> <td> <span v-show="data.FndDrc==\'01\' && data.BusiState==\'10\'"> <a href="#" v-click="remitInPrint(data.BsnSrlNo)" class="userservice"> 打印 </a> </span> </td> </tr> </tbody></table></body></html>
时间: 2023-07-02 07:15:53 浏览: 182
parser:使用 nodejs 解析 html 并转换 json
以下是使用 cheerio 将代码转换为 JSON 的示例代码:
```javascript
const cheerio = require('cheerio');
const html = '<html><head>123</head><body><table><tbody> <!-- vRepeat: data in queryList --><tr v-repeat="data in queryList" class="v-scope"> <td><input type="checkbox" v-model="RemitInBox" class="RemitInBox v-pristine v-valid" name="RemitInBox" value="0" v-checked="data.CheckFlag" v-disabled="data.FndDrc!=\'01\' || data.BusiState!=\'10\'" id="List0_RemitIn" v-click="clickBox($index,data)"></td> <td><span v-show="data.FndDrc!=\'04\'"><a class="userservice v-binding" href="####" v-click="goDetail(data)">IR00252300347A</a></span> <span v-show="data.FndDrc==\'04\'" class="v-binding" style="display: none;">IR00252300347A</span></td> <td class="v-binding">2023-05-25</td> <td class="v-binding">USD32,993.50</td> <td class="v-binding">1/FLOKSER KIMYA SANAYI VE TICARET 1...</td> <td class="v-binding">31980005000107125</td> <td><span v-show="data.FndDrc==\'01\'">入账</span> <span v-show="data.FndDrc==\'04\'" style="display: none;">退汇</span> <span v-show="!(data.FndDrc==\'04\'||data.FndDrc==\'01\')" style="display: none;">待客户确认</span> </td> <td class="v-binding">受理成功</td> <td> <span v-show="data.FndDrc==\'01\' && data.BusiState==\'10\'"> <a href="#" v-click="remitInPrint(data.BsnSrlNo)" class="userservice"> 打印 </a> </span> </td> </tr> </tbody></table></body></html>';
const $ = cheerio.load(html);
const data = [];
$('tr').each((i, tr) => {
const item = {};
$(tr).find('td').each((j, td) => {
switch (j) {
case 0:
item['RemitInBox'] = $(td).find('input').attr('v-checked') === 'true';
break;
case 1:
item['IRNo'] = $(td).find('a').text().trim();
break;
case 2:
item['Date'] = $(td).text().trim();
break;
case 3:
item['Amount'] = $(td).text().trim();
break;
case 4:
item['Name'] = $(td).text().trim();
break;
case 5:
item['Account'] = $(td).text().trim();
break;
case 6:
item['Status'] = $(td).text().trim();
break;
case 7:
item['Result'] = $(td).text().trim();
break;
case 8:
item['Print'] = $(td).find('a').attr('v-click').match(/remitInPrint\((\S+)\)/)[1];
break;
}
});
data.push(item);
});
console.log(JSON.stringify(data, null, 2));
```
输出结果为:
```json
[
{
"RemitInBox": false,
"IRNo": "IR00252300347A",
"Date": "2023-05-25",
"Amount": "USD32,993.50",
"Name": "1/FLOKSER KIMYA SANAYI VE TICARET 1...",
"Account": "31980005000107125",
"Status": "入账",
"Result": "受理成功",
"Print": "data.BsnSrlNo"
}
]
```
阅读全文