如何分割从网站抓取的文字?

问题描述 投票:1回答:1

在我要抓取的网站上,所有信息都在同一类.panel-row-text下。我不确定如何拆分此信息,以便仅将其显示在相关标题下,因为现在每一行将显示所有数据。

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

    const url = 'https://www.lseg.com/resources/1000-companies-inspire/2018-report-1000-companies-uk/search-1000-companies-uk-2018?results_per_page=100';

    axios(url)
      .then(response => {
        const html = response.data;
        const $ = cheerio.load(html);
        const dataTable = $('.tabular-data-panel > ul');
        const companyData= [];        
        //console.log(dataTable.length);

        dataTable.each(function(){
            const companyName = $(this).find('.panel-row-text').text();
            const website = $(this).find('.panel-row-text').text();
            const sector = $(this).find('.panel-row-text').text();
            const region = $(this).find('.panel-row-text').text();
            const revenueBand = $(this).find('.panel-row-text').text();

            companyData.push({
                companyName,
                website,
                sector,
                region,
                revenueBand,
            });

        });

        console.log(companyData);

      })
      .catch(console.error);

javascript web-scraping
1个回答
1
投票

您可以聪明地查询每个字段关联的标签。您可以简单地先查询标签,然后使用.next()函数获取相关标签的值。

注意:我添加了一个名为camelcase的额外包,以使查询的标签/属性更易于阅读。

const axios = require('axios');
const cheerio = require('cheerio');
const camelCase = require('camelcase'); // added this to make properties readable

// use async / await feature
async function scrape(url) {

    // get html page
    const { data } = await axios.get(url);

    // convert html string to cheerio instance
    const $ = cheerio.load(data);

    // query all list items
    return $('.tabular-data-panel > ul')
        // convert cheerio collection to array for easier manipulation
        .toArray()
        // transform each item into proper key values
        .map(list => $(list)
            // query the label element
            .find('.panel-row-title')
            // convert to array for easier manipulation
            .toArray()
            // use reduce to create the object
            .reduce((fields, labelElement) => {
                // get the cheerio instance of the element
                const $labelElement = $(labelElement);
                // get the label of the field
                const key = $labelElement.text().trim();
                // get the value of the field
                const value = $labelElement.next().text().trim();
                // asign the key value into the reduced object
                // note that we used camelCase() to make the property easy to read
                fields[camelCase(key)] = value;
                // return the object
                return fields;
            }, {})
        );


}

async function main() {
    const url = 'https://www.lseg.com/resources/1000-companies-inspire/2018-report-1000-companies-uk/search-1000-companies-uk-2018?results_per_page=100';
    const companies = await scrape(url);
    console.log(companies);
}

main();
© www.soinside.com 2019 - 2024. All rights reserved.