How to perform indexing in Cheerio for web scraping

Viewed 1111

I am using Cheerio for web scraping, I have used bs4 earlier.

I want to scrape https://rera.kerala.gov.in/rera_project_details this website; in Python to scrape table we can use findall("tr")[0] to get first <tr>.

But how to perform same in Cheerio?

Below is my code:

var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');


const url = "https://rera.kerala.gov.in/rera_project_details";

const arr = [];
request({method:"GET",url}, function(err, res, body){
if (res.statusCode==200){

    let $  = cheerio.load(body);
    const getID = $("#block-zircon-content");
    const tbody = getID.find('tbody');
    tbody.each((i, el)=>{
    const ff = $(el).find("tr");
    console.log(ff.html());//it returns first tr
    //how to get 2 tr so that i can get td of second tr and can inde on td also
    })
    

}}
)

If I loop over it returns all tr , now how to index on each td so that in last column of table I can get a link to get pdf?

Edit

I have reached till here but how to get list of td elements in tr:

    const getID = $(".views-table");
    
    const getBody = getID.find("tbody");
    
    const gettr = getBody.find("tr");
    const getfirsttr = $.html(gettr[0]);//it gives me first tr
    const getfirsttd = getfirsttr.find("td")//does not work
3 Answers

To answer the index question:

$('tr').eq(n)

will give you the nth tr as a cheerio object. and

$('tr')[n]

will give it as a parse5 object

You should be able to use a selector that will give you all the elements from the required table. Once you have the elements you can access their properties, children etc.

const url = "https://rera.kerala.gov.in/rera_project_details";
request({method:"GET",url}, function(err, res, body) {
    if (res.statusCode==200) {
        let $ = cheerio.load(body);
        // Get all td elements from the table.
        let tdElements = $("#block-zircon-content tbody tr td").map((i, el)=>{
            return el;
        }).toArray();
        console.log(`<td> list: Found ${tdElements.length} elements..`);
        console.log("tdElements[0]:", tdElements[0]);
        console.log("tdElements[0]: (html)", $.html(tdElements[0]))
    }}
);

To simply find all td elements in the table using .find() we can try:

const trElements = $("#block-zircon-content tbody").find("tr");
const tdElements = trElements.find("td").toArray();
console.log(`first td:`, tdElements[0]);

all right after doing research and and help above from terry i have understood how it works.. all cheerio functions works on selector html not on text..

below is my code in case any other beginner like me is using cheerio and stuck

var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
// const { get } = require('request');
// const { EACCES } = require('constants');


const url = "https://rera.kerala.gov.in/rera_project_details";

const arr = [];
request({method:"GET",url}, function(err, res, body){
if (res.statusCode==200){

    let $  = cheerio.load(body);
    // this is a selector  
    const getID = $(".views-table");
    
    const getBody = getID.find("tbody");
    
    const gettr = getBody.find("tr");
    gettr.each((index, element)=>{
        // if i use normal element it will be treated as normal text but children are avaiable
        //ON SELECTORS WE CAN APPLY ALL FUNCTIONS
        var std = $(element).find("td")
        let number = $(std[0]).contents().text();
        let ReraNumbers = $(std[1]).contents().text();
        let name = $(std[2]).contents().text().trim()
        // difference between tohtml and html is $.html retunr html tag
        // to html returns html content
    })
    

    //        const tdElements= gettr.find("td").toArray();
    // console.log(tdElements[2].children[0].data.trim())

    // let tdElements = $("#block-zircon-content tbody tr td").map((i, el)=>{
    //     return el;
    // }).toArray();
    // console.log(`<td> list: Found ${tdElements.length} elements..`);
    // console.log("tdElements[0]:", tdElements[0]);
    // console.log("tdElements[0]: (html)", $.html(tdElements[0]))


}}
)
Related