How to create a new cheerio instance $?

I need to instatiate a new cheerio object, i'm doing a search in a set o elements and need to select just one element for further processing, my actual code is:
function getOrigin($: typeof cheerioModule) {
let origin = ""
const specElements = $('#product_specs table tr').toArray()
for (const spec of specElements) {
const _$ = cheerioModule.load(spec)
const specTitle = _$(".attrib").text().trim()
if(specTitle.includes("제조국")){
origin = _$(".attrib-val").text().trim()
break
}
}
return origin
}
function getOrigin($: typeof cheerioModule) {
let origin = ""
const specElements = $('#product_specs table tr').toArray()
for (const spec of specElements) {
const _$ = cheerioModule.load(spec)
const specTitle = _$(".attrib").text().trim()
if(specTitle.includes("제조국")){
origin = _$(".attrib-val").text().trim()
break
}
}
return origin
}
but i'm not sure if that is the right way to instantiate a new cheeio object in crawlee.
1 Reply
grumpy-cyan
grumpy-cyan3y ago
You don't need to use load(). Instead of doing this:
const _$ = cheerioModule.load(spec);
const _$ = cheerioModule.load(spec);
Just do this using the $ already provided to you by Crawlee:
const elem = $(spec);
const elem = $(spec);
It will return a Cheerio object scoped only to that spec element. 😄

Did you find this page helpful?