const html =`<tbody> <tr> <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>` const cheerio= require('cheerio') const $ = cheerio.load(html) console.log($) const result = []; $('tr').each((i, tr) => { const tds = $(tr).find('td'); const data = { RemitInBox: $(tds[0]).find('input').attr('v-checked') === 'true', IRNumber: $(tds[1]).find('a').text(), Date: $(tds[2]).text(), Amount: $(tds[3]).text(), Account: $(tds[4]).text(), AccountNumber: $(tds[5]).text(), Status: $(tds[6]).text(), Result: $(tds[7]).text(), Print: $(tds[8]).find('a').length > 0, }; result.push(data); }); console.log(result);
时间: 2024-01-03 17:07:24 浏览: 74
这段代码使用了 Node.js 的 Cheerio 模块来对 HTML 进行解析和提取数据。它首先将 HTML 字符串赋值给变量 html,然后使用 Cheerio 的 load 方法将其转换为一个可操作的 jQuery 对象 $。接着使用 $ 对象的 find 和 text 方法来提取表格中每一行的数据,并将其存储在一个数组中。最后,打印结果数组。
相关问题
nodejs 将以下代码转换为json <html><head>123</head><body><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> </body></html>
抱歉,同样地,该代码也不符合 JSON 格式。建议您使用 HTML 解析器将其解析成一个 DOM 对象,然后再使用相关的方法将其转换为 JSON 格式。以下是一个示例代码,使用了 Node.js 中的 `cheerio` 模块来解析 HTML 并将其转换为 JSON 格式:
```
const cheerio = require('cheerio');
const html = '<html><head>123</head><body><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> </body></html>';
const $ = cheerio.load(html);
const tbody = $('tbody');
const result = [];
tbody.find('tr').each((i, tr) => {
const data = {};
$(tr).find('[v-repeat], [v-model]').each((j, el) => {
const name = $(el).attr('v-repeat') || $(el).attr('v-model');
const value = $(el).attr('v-bind:value') || $(el).val() || $(el).text();
data[name] = value;
});
result.push(data);
});
console.log(result);
```
该代码会输出一个包含了表格数据的 JSON 数组。
使用cheerio将以下代码转换为json <table><!-- vRepeat: data in queryList --><tbody> <tr> <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>
以下是使用cheerio将代码转换为json的方法:
```javascript
const cheerio = require('cheerio');
const html = '<table><!-- vRepeat: data in queryList --><tbody> <tr> <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>';
const $ = cheerio.load(html);
const data = [];
$('table tbody tr').each((i, elem) => {
const row = {};
$(elem).find('td').each((j, td) => {
const value = $(td).text().trim();
switch (j) {
case 1:
row['code'] = value;
break;
case 2:
row['date'] = value;
break;
case 3:
row['amount'] = value;
break;
case 4:
row['name'] = value;
break;
case 5:
row['account'] = value;
break;
case 6:
row['status'] = value;
break;
case 7:
row['result'] = value;
break;
default:
break;
}
});
data.push(row);
});
console.log(JSON.stringify(data, null, 2));
```
输出结果为:
```json
[
{
"code": "IR00252300347A",
"date": "2023-05-25",
"amount": "USD32,993.50",
"name": "1/FLOKSER KIMYA SANAYI VE TICARET 1...",
"account": "31980005000107125",
"status": "入账",
"result": "受理成功"
}
]
```
阅读全文