【问题标题】:Scraping all rows from table using cheerio使用cheerio从表中抓取所有行
【发布时间】:2021-09-08 20:52:23
【问题描述】:

我正在尝试从网页 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);
    });

【问题讨论】:

    标签: node.js web-scraping cheerio


    【解决方案1】:

    这似乎给出了你想要的数据:

    const axios = require("axios");
    const cheerio = require("cheerio");
    
    axios.get("https://www.barchart.com/stocks/quotes/aapl/performance")
      .then(({data}) => {
        const $ = cheerio.load(data);
        const rows = [];
        const sel = ".bc-symbol-performance-widget:nth-child(1) table tr";
        $(sel).each(function (i, e) {
          const row = [];
          rows.push(row);
          $(this).find("th").each(function (i, e) {
            row.push($(this).text().trim());
          });
          $(this).find("td").each(function (i, e) {
            row.push($(this).text().trim());
          });
        });
        console.table(rows);
      })
    ;
    

    输出:

    ┌─────────┬───────────┬─────────────────┬─────────────────────┐
    │ (index) │     0     │        1        │          2          │
    ├─────────┼───────────┼─────────────────┼─────────────────────┤
    │    0    │ 'Period'  │ 'Made New high' │ 'Percent From Last' │
    │    1    │  '5-Day'  │   '2  times'    │      '-5.27%'       │
    │    2    │ '1-Month' │   '8  times'    │      '-5.27%'       │
    │    3    │ '3-Month' │   '24  times'   │      '-5.27%'       │
    │    4    │ '6-Month' │   '26  times'   │      '-5.27%'       │
    │    5    │   'YTD'   │   '14  times'   │      '-5.27%'       │
    │    6    │ '52-Week' │   '25  times'   │      '-5.27%'       │
    │    7    │ '2-Year'  │   '91  times'   │      '-5.27%'       │
    │    8    │ '3-Year'  │   '40  times'   │      '-2.03%'       │
    │    9    │ '5-Year'  │   '76  times'   │      '-2.03%'       │
    │   10    │ '10-Year' │   '45  times'   │      '-2.03%'       │
    │   11    │ '20-Year' │   '88  times'   │      '-2.03%'       │
    └─────────┴───────────┴─────────────────┴─────────────────────┘
    

    这给出了第一张桌子,新高。如果您想要 New Lows 表,可以尝试选择器 ".bc-symbol-performance-widget:nth-child(3) table tr"

    我使用 Axios 是因为它很方便,但 node-fetch 的工作原理是一样的(我认为 Cheerio 是问题,而不是请求)。

    【讨论】:

    • 这行得通。我在解析表格和识别正确的选择器字符串时遇到了困难。我能够用你的例子做到这一点。非常感谢。
    猜你喜欢
    • 2020-05-29
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    相关资源
    最近更新 更多