Я хочу разобрать значения таблицы HTML в объект JSON с помощью CheerioJS.
Я изо всех сил пытаюсь освоить API-интерфейс Cheerio, но я нашел ужасное запутанное решение.
Я пытаюсь разобрать этот HTML
<table summary="Account summary" class="accounts-table">
<thead>
<tr>
<th>Accounts</th>
<th>Total value</th>
<th>Available</th>
<th>Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<td>
Total
</td>
<td>
£TOTALAMOUNT
</td>
<td>
£CASH
</td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<td style="white-space: normal">
<a href="https://awebsitehere.co.uk/account_summary/account/22" title="View your Stocks ISA"
class="product-name">
Stocks ISA
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/account_summary/account/22" title="View your Stocks ISA">
ISA-VAL
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/amount_available/account/22"
title="View cash summary for your Stocks ISA">
ISA-CASH
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/topup/account/22" title="Top up your Stocks ISA"
class="top-up-button">
Top up
</a>
<a href="https://awebsitehere.co.uk/topup/account/22l" title="Place a deal in your Stocks ISA"
class="deal-button">
</a>
</td>
</tr>
<tr>
<td style="white-space: normal">
<a href="https://awebsitehere.co.uk/account_summary/account/26" title="View your Junior ISA"
class="product-name">
Junior ISA
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/my-accounts/account_summary/account/26"
title="View your Junior ISA">
JUNIOR-VAL
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/my-accounts/amount_available/account/26"
title="View cash summary for your Junior ISA">
JUNIOR-CASH
</a>
</td>
<td>
</td>
</tr>
<tr>
<td style="white-space: normal">
<a href="https://awebsitehere.co.uk/my-accounts/account_summary/account/98"
title="View your Stocks Account" class="product-name">
Stocks Account
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/my-accounts/account_summary/account/98"
title="View your Stocks Account">
STOCKS-VAL
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/my-accounts/amount_available/account/98"
title="View cash summary for your stocks Account">
STOCKS-CASH
</a>
</td>
<td>
<a href="https://awebsitehere.co.uk/my-accounts/stock_and_fund_search/account/98/action/deal"
title="Place a deal in your stocks Account" class="deal-button">
<span style="padding-right:8px;">Deal</span>
</a>
</td>
</tr>
</tbody>
</table>
В объект JSON, который выглядит следующим образом:
{
"accounts": {
"Stocks ISA": {
"investments":
"total value": stocks-val
"cash": stocks-cash,
"link": "the href attached to this account"
},
"Junior ISA": {
"investments":
"total value": junior-val,
"cash": junior-cash,
"link": "the href attached to this account"
},
"stocks account": {
"investments":
"total value": stocks-val,
"cash": stocks-cash,
"link": "the href attached to this account"
}
}
}
Это код, который я пытался до сих пор, ноЯ изо всех сил пытаюсь обойти HTML-обход.
const $ = cheerio.load(body)
$('table[class="accounts-table"] tbody tr').each(
function (i, element) {
//@ts-ignore
let children = $(this).children()
children.each(
function (i, elem) {
//@ts-ignore
let children = $(this).children().text().trim()
}
)
}
)
Я был бы очень признателен всем, кто мог бы указать мне правильное направление!