Chris Rutherford
Chris Rutherford

Reputation: 1672

Getting text from Table Cheerio

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.

Upvotes: 4

Views: 7598

Answers (2)

Chiller
Chiller

Reputation: 9738

.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())

Upvotes: 7

Marcos Casagrande
Marcos Casagrande

Reputation: 40444

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());
});

Upvotes: 4

Related Questions