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] } }
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With