Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cheerio giving strange results

This is a simple cheerio app. The tag has a class named product-link and i want to access its href, but when i console log this, i am not getting any html. Can anyone shade a light on what is going on and how to get the data i want from this??

let holder = $('.product-link');
console.log(holder);

Result->

initialize {
  options:
   { withDomLvl1: true,
     normalizeWhitespace: false,
     xml: false,
     decodeEntities: true },    
  _root:
   initialize {
     '0':
      { type: 'root',
        name: 'root',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object: null prototype] {},
        'x-attribsNamespace': [Object: null prototype] {},
        'x-attribsPrefix': [Object: null prototype] {},
        children: [Array],
        parent: null,
        prev: null,
        next: null },
     options:
      { withDomLvl1: true,
        normalizeWhitespace: false,
        xml: false,
        decodeEntities: true },
     length: 1,
     _root: [Circular] },
  length: 0,
  prevObject:
   initialize {
     '0':
      { type: 'root',
        name: 'root',
        namespace: 'http://www.w3.org/1999/xhtml',
        attribs: [Object: null prototype] {},
        'x-attribsNamespace': [Object: null prototype] {},
        'x-attribsPrefix': [Object: null prototype] {},
        children: [Array],
        parent: null,
        prev: null,
        next: null },
     options:
      { withDomLvl1: true,
        normalizeWhitespace: false,
        xml: false,
        decodeEntities: true },
     length: 1,
     _root: [Circular] } }
like image 466
Ashok Avatar asked Apr 24 '19 17:04

Ashok


1 Answers

cheerio has an API similar to that of jQuery. so when you call this function $('selector') cheerio will return you an object with a lot of methods and fields to get attributes or find child nodes etc. So that's why it does not return the html. however, if you want to see the underlining HTML then you can use $('selector').html().

If you want to access an attribute you can use attr() method.

so now your code will be

const holder = $('.product-link');
const href = holder.attr('href');

You can reference the documentation from

https://github.com/cheeriojs/cheerio

https://api.jquery.com/ (since cheerio is designed to be similar to jQuery)

like image 98
priyansh gupta Avatar answered Sep 22 '22 23:09

priyansh gupta