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

167
Visualizações
How to crawling using Node.js

I can't believe that I'm asking an obvious question, but I still get the wrong in console log.

Console shows crawl like "[]" in the site, but I've checked at least 10 times for typos. Anyways, here's the javascript code.

I want to crawl in the site.

This is the kangnam.js file :

const axios = require('axios');
const cheerio = require('cheerio');
const log = console.log;

const getHTML = async () => {
    try {
        return await axios.get('https://web.kangnam.ac.kr', {
            headers: {
                Accept: 'text/html'
            }
        });
    } catch (error) {
        console.log(error);
    }
};

getHTML()
    .then(html => {
    let ulList = [];
    const $ = cheerio.load(html.data);
    const $allNotices = $("ul.tab_listl div.list_txt");
    
    $allNotices.each(function(idx, element) {
        ulList[idx] = {
            title : $(this).find("list_txt title").text(),
            url : $(this).find("list_txt a").attr('href')
        };
    });
    
    const data = ulList.filter(n => n.title);
    return data;
}). then(res => log(res));

I've checked and revised at least 10 times Yet, Js still throws this result :

root@goorm:/workspace/web_platform_test/myapp/kangnamCrawling(master)# node kangnam.js
[]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Mate, I think the issue is you're parsing it incorrectly.

$allNotices.each(function(idx, element) {
    ulList[idx] = {
        title : $(this).find("list_txt title").text(),
        url : $(this).find("list_txt a").attr('href')
    };
});

The data that you're trying to parse for is located within the first index of the $(this) array, which is really just storing a DOM Node. As to why the DOM stores Nodes this way, it's most likely due to efficiency and effectiveness. But all the data that you're looking for is contained within this Node object. However, the find() is superficial and only checks the indexes of an array for the conditions you supplied, which is a string search. The $(this) array only contains a Node, not a string, so when you you call .find() for a string, it will always return undefined.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

You need to first access the initial index and do property accessors on the Node. You also don't need to use $(this) since you're already given the same exact data with the element parameter. It's also more efficient to just use element since you've already been given the data you need to work with.

  $allNotices.each(function(idx, element) {
      ulList[idx] = {
          title : element.children[0].attribs.title,
          url : element.children[0].attribs.href
      };
  });

This should now populate your data array correctly. You should always analyze the data structures you're parsing for since that's the only way you can correctly parse them. Anyways, I hope I solved your problem!

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