Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
Parsing html with cheerio

I used cheerio for the first time today This is a simplified version of the html source I want.

<div id="country-table">
  <!-- div duplicate cause style -->

  <div>
    <div>
      <table>
        <tbody>
          <tr>
            <td>1</td>
            <td>USA</td>
            <td>1.6</td>
            <td>75.8</td>
            <td>132,000</td>
          </tr>
          <tr>
            <td>2</td>
            <td>INDIA</td>
            <td>12123</td>
            <td>1322</td>
            <td>123213</td>
          </tr>
          <tr>
            <td>3</td>
            <td>BRAZIL</td>
            <td>3123</td>
            <td>213123</td>
            <td>134</td>
          </tr>
          <tr>
           <!-- and more... -->
        </tbody>
      </table>
    </div>
  </div>
</div>

and i tried to this:

const axios = require("axios").default;
const cheerio = require("cheerio").default;
axios.get("https://coronaboard.kr").then((html) => {
  const arr = [];
  const $ = cheerio.load(html.data, { xml: true, xmlMode: true });
  const data = $("#country-table>div>div>table>tbody").each((index, item) => {
    arr.push(item);
  });
  console.log(arr);
});

I want to put information in td into tr. ex){number:x,name:USA,confirmed:x,and more...}

If anyone knows how to do it, please answer me!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you're wanting to extract the data from the table, then this will help. Follow the comments to help you understand how it works.

var $ = cheerio.load(html.data);
// targets the specific table with a selector
var html_table = $('#country-table>div>div>table');
// gets table cell values; loops through all tr rows
var table_data = html_table.find('tr').map(function() {
  // gets the cells value for the row; loops through each cell and returns an array of values
  var cells = $(this).find('td').map(function() {return $(this).text().trim();}).toArray();
  // returns an array of the cell data collected
  return [cells];
}).toArray();
// output table data
console.log('table_data', table_data);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda