【问题标题】:Cheerio not finding table contentCheerio 找不到表格内容
【发布时间】:2026-01-19 13:35:01
【问题描述】:

我需要解析一个包含表格的 HTML。

<div>
<table id="tableID">
    <tr>
        <td class="tdClass">
            <span id="id1">Some data i need to access</span>
        </td>
        <td class="tdClass">
            <span id="id2">Some data i need to access</span>
        </td>
    </tr>
</table>
</div>

我在 NW.js 应用程序上使用cheerio。我不知道如何访问数据,我尝试过使用 span 的 id,但它不起作用。

div 包含在页面的正文中。

var $ = cheerio.load(html)
alert($('#id1').html())

当我试图提醒 span 的内容时,我得到了 null。

【问题讨论】:

  • 请同时发布您的 js 代码
  • 用我的 js 代码更新了我的帖子。
  • 您运行的是服务器端还是客户端?我没有使用 NW.js,但是在 node.js 中你不能使用警报,你的代码在 jsbin 中工作正常:jsbin.com/nibanayoma/edit?html,js,output
  • 在完全相同的代码上,我得到一个空值。这很奇怪。我在 jsbin 上尝试了我的完整 html,它可以工作。我迷路了:(
  • 你是从哪里得到你的 html 的,你也可以发布吗?

标签: cheerio nw.js


【解决方案1】:

试试这个:

$ = cheerio.load(html, {normalizeWhitespace: false, xmlMode: true});

【讨论】: