使用cheerio 抓取表格的第n 个元素?

我想刮这张桌子的风柱https://www.bmreports.com/bmrs/?q=generation%2Ffueltype&fbclid=IwAR2p2a84CaJhgzF-YOU4MzOS-cMuZUpeDmLHFkWZU7-NauRHrA1owwdsMog 我可以刮整个桌子,但我不知道怎么做对其进行排序。理想情况下,我只会刮掉一列。


const request = require('request');

const cheerio = require('cheerio');


request('https://www.bmreports.com/bmrs/?q=generation%2Ffueltype&fbclid=IwAR2p2a84CaJhgzF-YOU4MzOS-cMuZUpeDmLHFkWZU7-NauRHrA1owwdsMog', (error, response, html) =>

{

    if (!error && response.statusCode == 200)

    {

        const $ = cheerio.load(html);


        const table = $('.gen-fuel');

        var newArray = [];

        var windArray = [];

       // console.log(table.html());

        $('td').each((i, el) =>

        {

            const item = $(el).html();


            newArray.push(item);

            console.log(item)


        });

        const every7thValue = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);

        console.log(every7thValue(newArray, 7));

        //console.log(newArray);




    }

});


郎朗坤
浏览 66回答 1
1回答

喵喵时光机

像这样的东西怎么样const request = require('request');const cheerio = require('cheerio');request(    'https://www.bmreports.com/bmrs/?q=generation%2Ffueltype&fbclid=IwAR2p2a84CaJhgzF-YOU4MzOS-cMuZUpeDmLHFkWZU7-NauRHrA1owwdsMog',    (error, response, html) => {        if (!error && response.statusCode == 200) {            const $ = cheerio.load(html);            const winds = $('.gen-fuel tbody td:nth-child(8)')                .map(function () {                    return $(this).text();                })                .get();            console.log(winds);        }    });Cheerio 支持 CSS 选择器,凭借其强大的功能,您可以获得包含来自风列的所有值的数组
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript