使用cheerio从表中抓取所有行

hey*_*jid 2 javascript node.js web-scraping 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)

ggo*_*len 8

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

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

输出:

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

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

\n

我使用 Axios 因为我有它方便,但节点获取的工作原理是一样的(我认为 Cheerio 是问题,而不是请求)。

\n
\n

一年后重新审视这一点,选择器和表格布局发生了变化。这是与更新一起使用的新代码(第三列是空的,所以我用 删除它not):

\n
\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 (index) \xe2\x94\x82     0     \xe2\x94\x82        1        \xe2\x94\x82          2          \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82    0    \xe2\x94\x82 \'Period\'  \xe2\x94\x82 \'Made New high\' \xe2\x94\x82 \'Percent From Last\' \xe2\x94\x82\n\xe2\x94\x82    1    \xe2\x94\x82  \'5-Day\'  \xe2\x94\x82   \'2  times\'    \xe2\x94\x82      \'-5.27%\'       \xe2\x94\x82\n\xe2\x94\x82    2    \xe2\x94\x82 \'1-Month\' \xe2\x94\x82   \'8  times\'    \xe2\x94\x82      \'-5.27%\'       \xe2\x94\x82\n\xe2\x94\x82    3    \xe2\x94\x82 \'3-Month\' \xe2\x94\x82   \'24  times\'   \xe2\x94\x82      \'-5.27%\'       \xe2\x94\x82\n\xe2\x94\x82    4    \xe2\x94\x82 \'6-Month\' \xe2\x94\x82   \'26  times\'   \xe2\x94\x82      \'-5.27%\'       \xe2\x94\x82\n\xe2\x94\x82    5    \xe2\x94\x82   \'YTD\'   \xe2\x94\x82   \'14  times\'   \xe2\x94\x82      \'-5.27%\'       \xe2\x94\x82\n\xe2\x94\x82    6    \xe2\x94\x82 \'52-Week\' \xe2\x94\x82   \'25  times\'   \xe2\x94\x82      \'-5.27%\'       \xe2\x94\x82\n\xe2\x94\x82    7    \xe2\x94\x82 \'2-Year\'  \xe2\x94\x82   \'91  times\'   \xe2\x94\x82      \'-5.27%\'       \xe2\x94\x82\n\xe2\x94\x82    8    \xe2\x94\x82 \'3-Year\'  \xe2\x94\x82   \'40  times\'   \xe2\x94\x82      \'-2.03%\'       \xe2\x94\x82\n\xe2\x94\x82    9    \xe2\x94\x82 \'5-Year\'  \xe2\x94\x82   \'76  times\'   \xe2\x94\x82      \'-2.03%\'       \xe2\x94\x82\n\xe2\x94\x82   10    \xe2\x94\x82 \'10-Year\' \xe2\x94\x82   \'45  times\'   \xe2\x94\x82      \'-2.03%\'       \xe2\x94\x82\n\xe2\x94\x82   11    \xe2\x94\x82 \'20-Year\' \xe2\x94\x82   \'88  times\'   \xe2\x94\x82      \'-2.03%\'       \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n

尽管我使用的语法略有不同,但模式是相同的:映射行,然后为每一行创建一个单元格数组。

\n