【问题标题】:why cheerio.load() function is not working properly为什么cheerio.load() 函数不能正常工作
【发布时间】:2021-07-21 10:19:04
【问题描述】:
const cheerio = require('cheerio');

const HTML = `<tbody>
<tr>
    <th>
        Team Name
    </th>
    <th>
        Year
    </th>
    <th>
        Wins
    </th>
    <th>
        Losses
    </th>
</tr>
</tbody>
`;

const $ = cheerio.load(HTML);
const text = $('tr').text();
console.log(text);

这里我没有收到短信。为什么?? HTML 字符串中是否有任何错误? 我无法确定问题所在。请帮忙!!!

【问题讨论】:

标签: javascript node.js web-scraping cheerio


【解决方案1】:

&lt;tbody&gt; 元素必须是 &lt;table&gt; 元素的子元素,而您的元素不是。

在当前版本的 Cheerio 中,解析器无法从该错误中恢复。编写有效的 HTML。 (用&lt;table&gt;&lt;/table&gt; 包裹你的字符串就足够了。

(如果您降级到 Cheerio 0.22.0,那么那里的解析器可以处理它,但修复输入比使用过时的库更可取)。

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多