Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda