跳至主要内容

接口: CheerioOptions

Cheerio 接受的选项。

请注意,解析器特定的选项 *仅在* 使用相关解析器时 *才被识别*。

扩展

被扩展

属性

baseURI?

可选 baseURI: string | URL

文档的基准 URI。用于解析 hrefsrc 属性。

定义于

src/options.ts:42


onParseError?

可选 onParseError: null | ParserErrorHandler

解析错误的回调函数。

默认值

null

继承自

Parse5ParserOptions.onParseError

定义于

node_modules/parse5/dist/parser/index.d.ts:63


pseudos?

可选 pseudos: Record<string, string | (elem, value?) => boolean>

伪类的扩展点。

从名称映射到字符串或函数。

  • 字符串值是一个选择器,元素必须匹配才能被选中。
  • 函数使用元素作为其第一个参数,可选参数作为第二个参数调用。如果它返回 true,则该元素将被选中。

示例

const $ = cheerio.load(
'<div class="foo"></div><div data-bar="boo"></div>',
{
pseudos: {
// `:foo` is an alias for `div.foo`
foo: 'div.foo',
// `:bar(val)` is equivalent to `[data-bar=val s]`
bar: (el, val) => el.attribs['data-bar'] === val,
},
},
);

$(':foo').length; // 1
$('div:bar(boo)').length; // 1
$('div:bar(baz)').length; // 0

定义于

src/options.ts:81


quirksMode?

可选 quirksMode: boolean

文档是否处于怪癖模式?

这将导致 .className#id 不区分大小写。

默认值

false

定义于

src/options.ts:51


scriptingEnabled?

可选 scriptingEnabled: boolean

脚本标志。如果设置为 truenoscript 元素内容将被解析为文本。

默认值

true

继承自

Parse5ParserOptions.scriptingEnabled

定义于

node_modules/parse5/dist/parser/index.d.ts:40


sourceCodeLocationInfo?

可选 sourceCodeLocationInfo: boolean

启用源代码位置信息。启用后,每个节点(根节点除外)都将具有 sourceCodeLocation 属性。如果节点不是空元素,sourceCodeLocation 将是一个 ElementLocation 对象,否则它将是 Location。如果元素是由解析器隐式创建的(作为 树校正 的一部分),则其 sourceCodeLocation 属性将为 undefined

默认值

false

继承自

Parse5ParserOptions.sourceCodeLocationInfo

定义于

node_modules/parse5/dist/parser/index.d.ts:51


treeAdapter?

可选 treeAdapter: TreeAdapter<Htmlparser2TreeAdapterMap>

指定生成的树格式。

默认值

treeAdapters.default

继承自

Parse5ParserOptions.treeAdapter

定义于

node_modules/parse5/dist/parser/index.d.ts:57


xml?

可选 xml: boolean | HTMLParser2Options

希望解析 XML 时配置 htmlparser2 的推荐方法。

这将切换 Cheerio 以使用 htmlparser2。

默认值

false

定义于

src/options.ts:31


xmlMode?

可选 xmlMode: boolean

启用 xml 模式,这将切换 Cheerio 以使用 htmlparser2。

已弃用

请改用 xml 选项。

默认值

false

定义于

src/options.ts:39