使用 Cheerio

问题描述 投票:0回答:1

我想使用 Cheerio 从 HTML 文档中提取信息。 我无法更改 HTML 文件的内容,因为它具有外部来源。

我要提取的元素有一个类名,其中有一个点:

<span class='prop-data.size'> the content </span>

如果我使用 Cheerio 中的类名作为选择器,我不会取回该元素。

$('.prop-data.size').text() <-- gives null

在 Cheerio 中是否可以通过类名中带点的来选择元素?

我可以避开点吗?

javascript node.js cheerio
1个回答
0
投票

是的,逃避

.

const cheerio = require("cheerio"); // ^1.0.0-rc.12

const html = "<span class='prop-data.size'> the content </span>";

const $ = cheerio.load(html);
console.log($(".prop-data\\.size").text()); // => the content
© www.soinside.com 2019 - 2024. All rights reserved.