Working on a web scraping project, and I'm having trouble getting some of the data in a uniform way. The page has a two-column table, and I need to only grab the text of the second column to run compile the values.
I'm working on it like this:
const rq = require('request');
const cheerio = require('cheerio');
rq(url, (err, res, html) => {
let $ = cheerio.load(html);
$('#table-id > tbody > tr > td.data').toArray().map(item => {
console.log(item.text());
});
});
But I'm getting an error that .text()
is not a function.
.text()
is a cheerio method so to use it you need to make the item a cheerio element
this should work:
console.log($(item).text())
You have to wrap item
with $()
, to convert it to a cheerio
element.
$('#table-id > tbody > tr > td.data').toArray().map(item => {
console.log($(item).text());
});
You can also use .each and drop toArray
and map
. And use $(this)
to reference the current element.
$('#table-id > tbody > tr > td.data').each(() => {
console.log($(this).text());
});
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