Search code examples
javascripthtmlnode.jscheerio

NodeJS Cheerio Scraping li tags always returns NULL


I'm trying to zero in on the URL contained within the li tags on the page in my URL variable. It should be simple, but I can't get it to work. I get the correct number of elements, but they are all blank. text() returns '' & html() returns null. What am I doing wrong here?

const cheerio = require('cheerio');
const request = require('request');

function getHistory(){
  let url = 'http://coinmarketcap.com/historical/';
  request(url,(error,response,html)=>{
    var $ = cheerio.load(html);
    $('li.text-center').each((i,element)=>{
      var omg = $(this).html();
      console.log(omg);
    });
  });
}

Solution

  • Because this code is in an arrow function (which retains the lexical value of this, not what .each() sets it to), the value of this is not set to what you want it to be. If you change this:

    var omg = $(this).html();
    

    to this:

    var omg = $(element).html();
    

    You will see the HTML you were expecting.

    Note, you may also be able to change the arrow function to a regular function and then whatever value of this that .each() sets will be in effect.


    If what you really want is the href, then you should target the <a> tag with your selector and get the actual href attribute from it. You could do that like this:

    function getHistory(){
      let url = 'http://coinmarketcap.com/historical/';
      request(url,(error,response,html)=>{
        let $ = cheerio.load(html);
        $('li.text-center a').each((i,element)=>{
          let omg = $(element).attr('href');
          console.log(omg);
        });
      });
    }