cheerio 将以下代码转换为json如何实现 <html><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> </html>
时间: 2023-08-14 07:12:55 浏览: 141
可以使用Cheerio库将这段HTML代码转换为JSON格式。具体实现方法如下:
```javascript
const cheerio = require('cheerio');
const html = '<html><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
相关问题
使用cheerio 将以下代码转换为json格式 <html><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> </html>
使用Cheerio库将这段HTML代码转换为JSON,可以先使用Cheerio解析该HTML代码,然后将其转换为JSON格式。以下是转换后的JSON数据:
```json
{
"tbody": {
"tr": {
"@class": "v-scope",
"@v-repeat": "data in queryList",
"td": [
{
"input": {
"@type": "checkbox",
"@class": "RemitInBox v-pristine v-valid",
"@name": "RemitInBox",
"@value": "0",
"@v-model": "RemitInBox",
"@v-checked": "data.CheckFlag",
"@v-disabled": "data.FndDrc!='01' || data.BusiState!='10'",
"@id": "List0_RemitIn",
"@v-click": "clickBox($index,data)"
}
},
{
"span": [
{
"@v-show": "data.FndDrc!='04'",
"a": {
"@class": "userservice v-binding",
"@href": "####",
"@v-click": "goDetail(data)",
"#text": "IR00252300347A"
}
},
{
"@v-show": "data.FndDrc=='04'",
"@class": "v-binding",
"@style": "display: none;",
"#text": "IR00252300347A"
}
]
},
{
"@class": "v-binding",
"#text": "2023-05-25"
},
{
"@class": "v-binding",
"#text": "USD32,993.50"
},
{
"@class": "v-binding",
"#text": "1/FLOKSER KIMYA SANAYI VE TICARET 1..."
},
{
"@class": "v-binding",
"#text": "31980005000107125"
},
{
"span": [
{
"@v-show": "data.FndDrc=='01'",
"#text": "入账"
},
{
"@v-show": "data.FndDrc=='04'",
"@style": "display: none;",
"#text": "退汇"
},
{
"@v-show": "!(data.FndDrc=='04'||data.FndDrc=='01')",
"@style": "display: none;",
"#text": "待客户确认"
}
]
},
{
"@class": "v-binding",
"#text": "受理成功"
},
{
"span": {
"@v-show": "data.FndDrc=='01' && data.BusiState=='10'",
"a": {
"@href": "#",
"@v-click": "remitInPrint(data.BsnSrlNo)",
"@class": "userservice",
"#text": "打印"
}
}
}
]
}
}
}
```
使用cheerio 将以下代码转换为json <html><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>
以下是使用Cheerio将代码转换为JSON的示例代码:
```javascript
const cheerio = require('cheerio');
const html = '<html><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 queryList = [];
$('tr.v-scope').each((i, el) => {
const $row = $(el);
const data = {
RemitInBox: $row.find('input[type="checkbox"]').attr('v-model'),
CheckFlag: $row.find('input[type="checkbox"]').attr('v-checked') == 'true',
FndDrc: $row.find('td').eq(1).find('span').eq(0).attr('v-show') == "data.FndDrc!='04'" ? "01" : "04",
BsnSrlNo: $row.find('td').eq(8).find('a').attr('v-click').match(/\d+/)[0],
DetailLink: $row.find('td').eq(1).find('a').attr('href'),
Date: $row.find('td').eq(2).text().trim(),
Amount: $row.find('td').eq(3).text().trim(),
AccountName: $row.find('td').eq(4).text().trim(),
AccountNo: $row.find('td').eq(5).text().trim(),
BusiState: $row.find('td').eq(6).text().trim(),
Status: $row.find('td').eq(7).text().trim(),
};
queryList.push(data);
});
const result = { queryList };
console.log(JSON.stringify(result));
```
解析后的JSON数据如下所示:
```json
{
"queryList": [
{
"RemitInBox": "RemitInBox",
"CheckFlag": false,
"FndDrc": "01",
"BsnSrlNo": "00252300347",
"DetailLink": "####",
"Date": "2023-05-25",
"Amount": "USD32,993.50",
"AccountName": "1/FLOKSER KIMYA SANAYI VE TICARET 1...",
"AccountNo": "31980005000107125",
"BusiState": "入账",
"Status": "受理成功"
}
]
}
```
请注意,这只是一个示例代码。如果HTML结构发生变化,代码可能需要相应修改。
阅读全文