javascript - 如何使用cheerio枚举html表

标签 javascript html cheerio

我有一个标准的 HTML 表格,但我似乎找不到任何东西来立足并开始。 我的 table 是这样的:

    <div id="seriesDiv" style="margin-top: 0px;">
          <table class="tableFile2" summary="Results">
             <tbody><tr>
                <th scope="col" width="7%">Filings</th>
                <th scope="col" width="10%">Format</th>
                <th scope="col">Description</th>
                <th scope="col" width="10%">Filing Date</th>
                <th scope="col" width="15%">File/Film Number</th>
             </tr>
    <tr>
    <td nowrap="nowrap">4</td>
    <td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119051031/0001209191-19-051031-index.htm" id="documentsbutton">&nbsp;Documents</a></td>
    <td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-051031 Size: 9 KB            </td>
                <td>2019-09-27</td>
             <td></td>
             </tr>
    <tr class="blueRow">
    <td nowrap="nowrap">4</td>
    <td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119050363/0001209191-19-050363-index.htm" id="documentsbutton">&nbsp;Documents</a></td>
    <td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-050363 Size: 50 KB            </td>
                <td>2019-09-20</td>
             <td></td>
             </tr>

标准的东西——table、tbody、trs 和 tds

我已经尝试过

var rows = $("#seriesDiv").find("table");
console.log("in front loop ", rows.length);
for (var i = 0; rows.length; i++) {
    console.log("inside loop");
}

我尝试过:

var rows = $(".tableFile2").find("tr");
```

Can someone please show me how to do this?  I want to loop through TRs, and for each TR, I want to loop through TDs.

thank you

最佳答案

下面的代码将帮助您循环遍历每个 tr。另请注意,如果您仅为每个“tr”查找“tds”,则将找不到“th”。因此,你还必须寻找“ths”。请注意,下面的代码只是一个示例。因此,没有错误检查或数据评估或类似的东西。

const cheerio = require('cheerio');
var html = `
<div id="seriesDiv" style="margin-top: 0px;">
      <table class="tableFile2" summary="Results">
         <tbody><tr>
            <th scope="col" width="7%">Filings</th>
            <th scope="col" width="10%">Format</th>
            <th scope="col">Description</th>
            <th scope="col" width="10%">Filing Date</th>
            <th scope="col" width="15%">File/Film Number</th>
         </tr>
<tr>
<td nowrap="nowrap">4</td>
<td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119051031/0001209191-19-051031-index.htm" id="documentsbutton">&nbsp;Documents</a></td>
<td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-051031 Size: 9 KB            </td>
            <td>2019-09-27</td>
         <td></td>
         </tr>
<tr class="blueRow">
<td nowrap="nowrap">4</td>
<td nowrap="nowrap"><a href="/Archives/edgar/data/1087294/000120919119050363/0001209191-19-050363-index.htm" id="documentsbutton">&nbsp;Documents</a></td>
<td class="small">Statement of changes in beneficial ownership of securities<br>Acc-no: 0001209191-19-050363 Size: 50 KB            </td>
            <td>2019-09-20</td>
         <td></td>
         </tr>
`;

const $ = cheerio.load(html);

var tables = $("#seriesDiv").find("table");
var trs = $(tables).find("tr");

for ( let i = 0; i < trs.length; i++ ){
    let tds = $(trs[i]).find("td");
    let ths = $(trs[i]).find("th");
    for ( let j = 0; j < ths.length; j++ ){
      console.log($(ths[j]).text());
    }
    for ( let j = 0; j < tds.length; j++ ){
      console.log($(tds[j]).text());
    }
}

关于javascript - 如何使用cheerio枚举html表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58368553/

相关文章:

javascript - 经济拒绝 127.0.0.1 :443

javascript - 无法找出 MYSQL 查询来获得我想要的结果

html - 如何从 XML 和 XSLT 样式表输出 HTML 文件

javascript - 访问请求cheerios函数外部的变量

javascript - 用cheerio刮掉所有元素

javascript - 日期选择器上的自定义输入字段

javascript - Django 中的弹出窗体(Jquery)

php - 在 HTML 表格中显示嵌套数组

javascript - 如何使用 pdfMake 处理动态数据

node.js - 如何组合两个都需要监听端口的 Express 模块?