简体   繁体   中英

Can't display selectors content in Cheerio

I'm trying to extract a table from a website, and wanted to get all the columns first. After the request is made, I load the html into cheerio, but when I try to display the selector content, nothing appears on the console. What is confusing me is when I try the same selector directly on to the page console, it works and shows me all of them.

Here is the url I'm scraping.

Here is the cheerio selector I'm using to return the columns. The content I want is on the tag th with the class 'sorting'.

$('.sorting').each(function (index, element) {
                const $element = $(element);
                console.log($element.text());
            });

And here is the full code.

const request = require('request');
const cheerio = require('cheerio');

const fundsExplorerUrl = 'https://www.fundsexplorer.com.br/ranking';

request(fundsExplorerUrl,
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            const $ = cheerio.load(body);

            $('.sorting').each(function (index, element) {
                const $element = $(element);
                console.log($element.text());
            });
        }
    }
);

Thanks for helping!

In the raw HTML, there is no class called sorting because javascript is dynamically adding this class to dom so in this specific case by using following code you can gather the content of all th tags embedded in the thead tag of table tag.

const request = require('request-promise');
const cheerio = require('cheerio');

const url = 'https://www.fundsexplorer.com.br/ranking';

async function crawl() {
    const rawHtml = await request(url);
    const $ = cheerio.load(rawHtml);

    $('table thead tr th')
        .each( (index, element) => {
        console.log($(element).text());
    })
}

crawl();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM