简体   繁体   English

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

[英]Scraping the nth element of a table using cheerio?

i want to scrape the wind column of this table https://www.bmreports.com/bmrs/?q=generation%2Ffueltype&fbclid=IwAR2p2a84CaJhgzF-YOU4MzOS-cMuZUpeDmLHFkWZU7-NauRHrA1owwdsMog i can scrape the whole table, but i don't know how to sort it.我想刮这张桌子的风柱https://www.bmreports.com/bmrs/?q=generation%2Ffueltype&fbclid=IwAR2p2a84CaJhgzF-YOU4MzOS-cMuZUpeDmLHFkWZU7-NauRHrA1owwdsMog我知道怎么刮整个桌子对其进行排序。 Ideally i would just scrape the one column.理想情况下,我只会刮掉一列。

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



    }
});

How about something like像这样的东西怎么样

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 supports CSS selectors, with its power you have got array with all values from wind column Cheerio 支持 CSS 选择器,凭借其强大的功能,您可以获得包含来自列的所有值的数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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