跳至主要内容

接口: CheerioAPI()

一个查询函数,绑定到从提供的标记创建的文档。

还提供了一些辅助方法来处理整个文档。

扩展

  • 静态类型

CheerioAPI<T, S>(selector?, context?, root?, options?): Cheerio<S extends SelectorType ? Element : T>

一个查询函数,绑定到从提供的标记创建的文档。

还提供了一些辅助方法来处理整个文档。

类型参数

T extends AnyNode

S extends string

参数

selector?: S | BasicAcceptedElems<T>

在文档中查找的 selector,或者新 Cheerio 实例的内容。

context?: null | BasicAcceptedElems<AnyNode>

在根目录中查找的 selector,或者要查询的文档的内容。

root?: BasicAcceptedElems<Document>

可选的 HTML 文档字符串。

options?: CheerioOptions

返回值

Cheerio<S extends SelectorType ? Element : T>

示例

$('ul .pear').attr('class');
//=> pear

$('li[class=orange]').html();
//=> Orange

$('.apple', '#fruits').text();
//=> Apple

可选地,您也可以通过将字符串作为 selector 传递来加载 HTML

$('<ul id="fruits">...</ul>');

或者上下文

$('ul', '<ul id="fruits">...</ul>');

或者作为根目录

$('li', 'ul', '<ul id="fruits">...</ul>');

定义于

src/load.ts:66

已弃用

load()

load: (content, options?, isDocument) => CheerioAPI

在“加载”的 Cheerio 工厂函数上定义的 .load 静态方法已弃用。鼓励用户改用 Cheerio 模块导出的 load 函数。

参数

content: string | AnyNode | AnyNode[] | Buffer

options?: null | CheerioOptions

isDocument?: boolean = true

返回值

CheerioAPI

已弃用

使用 Cheerio 模块导出的 load 函数。

示例

const $ = cheerio.load('<h1>Hello, <span>world</span>.</h1>');

定义于

src/load.ts:103

其他

contains

重新导出 contains


merge

重新导出 merge


fn

fn: Cheerio<any>

模仿 jQuery 的原型别名,供插件作者使用。

定义于

src/load.ts:88

静态

extract()

extract<M>(this, map): ExtractedMap<M>

从文档中提取多个值,并将它们存储在一个对象中。

类型参数

M extends ExtractMap

参数

this: CheerioAPI

map: M

包含键值对的对象。键是要在对象上创建的属性的名称,值是要用于提取值的 selector。

返回值

ExtractedMap<M>

包含提取值的 객체.

继承自

StaticType.extract

定义于

src/static.ts:248


html()

html(this, options)

html(this, options?): string

呈现文档。

参数

this: CheerioAPI

options?: CheerioOptions

呈现器的选项。

返回值

string

呈现的文档。

继承自

StaticType.html

定义于

src/static.ts:58

html(this, dom, options)

html(this, dom?, options?): string

呈现文档。

参数

this: CheerioAPI

dom?: BasicAcceptedElems<AnyNode>

要呈现的元素。

options?: CheerioOptions

呈现器的选项。

返回值

string

呈现的文档。

继承自

StaticType.html

定义于

src/static.ts:67


parseHTML()

parseHTML(this, data, context, keepScripts)

parseHTML(this, data, context?, keepScripts?): AnyNode[]

将字符串解析为 DOM 节点数组。context 参数对 Cheerio 没有意义,但它被保留以与 jQuery 的 API 兼容。

参数

this: CheerioAPI

data: string

要解析的标记。

context?: unknown

将被忽略。如果是布尔值,它将用作 keepScripts 的值。

keepScripts?: boolean

如果为 false,则所有脚本将被删除。

返回值

AnyNode[]

解析的 DOM。

别名

Cheerio.parseHTML

参见

https://api.jqueryjs.cn/jQuery.parseHTML/

继承自

StaticType.parseHTML

定义在

src/static.ts:152

parseHTML(this, data)

parseHTML(this, data?): null

参数

this: CheerioAPI

data?: null | ""

返回值

null

继承自

StaticType.parseHTML

定义在

src/static.ts:158


root()

root(this): Cheerio<Document>

有时您需要使用顶层根元素。要查询它,可以使用 $.root()

参数

this: CheerioAPI

返回值

Cheerio<Document>

包装根节点的 Cheerio 实例。

示例

$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>

别名

Cheerio.root

继承自

StaticType.root

定义在

src/static.ts:203


text()

text(this, elements?): string

将文档呈现为文本。

这将返回传递元素的 textContent。结果将包括 <script><style> 元素的内容。要避免这种情况,请使用 .prop('innerText') 代替。

参数

this: void | CheerioAPI

elements?: ArrayLike<AnyNode>

要呈现的元素。

返回值

string

呈现的文档。

继承自

StaticType.text

定义在

src/static.ts:124


xml()

xml(this, dom?): string

将文档呈现为 XML。

参数

this: CheerioAPI

dom?: BasicAcceptedElems<AnyNode>

要呈现的元素。

返回值

string

呈现的文档。

继承自

StaticType.xml

定义在

src/static.ts:104