小编hey*_*jid的帖子

使用cheerio从表中抓取所有行

我正在尝试从网页https://www.barchart.com/stocks/quotes/aapl/performance上的价格表现表中抓取所有行, 它是折线图下方的表格。该表没有任何 id 或 class 属性。我试图从它包含的 div 中获取表格。下面是代码,但它没有打印 td 元素内的文本。

$ = await fetchData(performanceHistoryUrl);    
let performanceTableDiv = $(".bc-table-scrollable-inner") // Class of div which is enclosing table
    var childSelector = 'table' // table selector 
    var performanceTable = performanceTableDiv.find(childSelector)
    performanceTable.each((index, element) => {
            if (index === 0) return true;
            const tds = $(element).find("td");
            const colOne = $(tds[0]).text();
            const colTwo = $(tds[1]).text();
            const colThree = $(tds[2]).text();
            const tableRow = { colOne, colTwo, tableRow };
            console.log(tableRow);
    });
Run Code Online (Sandbox Code Playgroud)

javascript node.js web-scraping cheerio

2
推荐指数
1
解决办法
3512
查看次数

标签 统计

cheerio ×1

javascript ×1

node.js ×1

web-scraping ×1