How can I get the content of an element? I have tried using this
let $ = cheerio.load(response);
let products = $('.product-tile__title');
console.log(products.first().html()); // prints null to console
There is a list of elements having the class name "product-tile__title
" I want to get the text in the element. They are in h4 title elements which may make a difference.
Element HTML:
<h4 class="ais-hit--title product-tile__title">HP 15-EQ1048AU 15.6" HD Laptop (256GB)</h4>
Code:
var app = express();
app.get('/test', function(req, res) {
const options = {
url: `https://www.jbhifi.com.au/collections/computers-tablets/windows-laptops`,
headers: { 'User-Agent': 'Request-Promise' }
}
request(options)
.then((response, body) => {
process.stdout.write('testing 123');
console.log('testing console logger');
let $ = cheerio.load(response);
//let products = $('.product-tile__title');
let arr = $('.product-tile__title').toArray().map((x) => { return $(x).html()});
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
console.log(arr.length); // 0
$('.product-tile__title').each((i,element)=>{
var omg = $(element).html();
console.log(omg);
});
console.log(response);
res.write(response);
res.write('request complete');
res.end();
})
.catch((err) => {
console.log('error in request');
console.log(err);
})
})