简体   繁体   中英

What's the fastest way to parse this HTML table?

I'm trying to parse a table in Node that I get from a website. The table looks like this. I want to ignore the header and parseonly the actual transaction bodies.

        <tbody><tr class="dgHeader" style="font-weight:bold;">
            <th scope="col">Reference 1</th><th scope="col">Reference 2</th><th scope="col">Reference 3</th><th scope="col">Reference 4</th><th scope="col">Gross Amount</th><th scope="col">Discounts/Surcharges</th><th scope="col">Net Amount</th><th scope="col">Means of Payment</th><th scope="col">Form of Payment</th><th scope="col">Payment Folio</th><th scope="col">Branch</th><th scope="col">Time</th><th scope="col">Maturity Date</th><th scope="col">Payment date</th>             </tr><tr align="left">
            <td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblReferencia1">0000000000000000000000000000000X4D649G66</span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblReferencia2"></span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblReferencia3"></span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblReferencia4"></span>
                    </td><td align="right">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblImporteBruto">$40.00</span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblDescuentosRecargos">$0.00</span>
                    </td><td align="right">
                    <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblImporteNeto">$40.00</span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblMedioPago">Internet</span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblFormaPago">Cash</span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblFolioPago">45786172008896142466 </span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblSucursal">4578</span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblHora">01:48:59 p.m.</span>
                    </td><td>
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblFechaVencimiento">00/00/0000</span>
                    </td><td align="left">
                        <span id="ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblFechaPago">20/06/2016</span>
                    </td>           </tr>       </tbody>

I've been using Cheerio, but having a hard time getting the id tags to get the data from the table.

This ended up solving it, and allowing me to obtain the reference code pretty easily.

$ = cheerio.load(str, {
    ignoreWhitespace: true
  });

$('tr').each(function(i, tr){   
    var reference = $('#ctl00_Contentplaceholder1_gvConcentracionPagos_ctl02_lblReferencia1').text())
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM