Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 778
Chris Rutherford Avatar asked Jan 28 '23 01:01

Chris Rutherford


2 Answers

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

like image 138
Chiller Avatar answered Jan 31 '23 09:01

Chiller


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());
});
like image 31
Marcos Casagrande Avatar answered Jan 31 '23 07:01

Marcos Casagrande