外观
HTML解析.js
js
/**
* 公开入口:@kodet/core 的 html:原生HTML/CSS/XPath/批量extract。
* 返回 HtmlDocument/HtmlNode/HtmlQuery 实例,不是浏览器 DOM,不执行网页 JS。
*/
/**
* 调用写法:
*
* const document = await html.parse("<main><a href='/book'>书名</a></main>", {
* baseURL: "https://example.invalid/",
* });
* try {
* const names = await document.select("a").texts();
* } finally {
* await document.close();
* }
*
* 也可 await html.parse(response, parseOptions),原生直接消费 HTTP 正文。
* 与 text/json/bytes/body 共用一次性消费规则,不能先读完再解析同一个 response。
*/
const parseOptions = {
baseURL: "https://example.invalid/", // 可选;解析响应时默认使用响应 URL。
encoding: "utf-8", // 用于字节输入(含Response);输入是已解码字符串时不能传。
maxBytes: 5242880, // 默认且最大 5 MiB。
};
/**
* 查询调用写法:
*
* const query = document.select("main").select("a");
* const xpath = document.xpath("//a/text()");
*
* const first = await query.first(); // HtmlNode | string | null。
* const all = await query.all(); // (HtmlNode | string)[]。
* const nodes = await query.nodes(); // 仅 HtmlNode[],不包含 XPath 文本/属性结果。
* const text = await query.text();
* const texts = await query.texts();
* const href = await query.attr("href");
* const hrefs = await query.attrs("href");
* const innerHtml = await query.html();
* const htmls = await query.htmls();
*
* select/xpath 可以交叉链式查询,最多 16 步;真正取结果时才执行查询。
* 得到的 HtmlNode 也支持上述查询方法;first 为 string/null 时不要当节点使用。
*/
const queryResults = { // 各方法结果形状的汇总说明,不是 SDK 返回的统一外层对象。
text: "书名", // string | null。
texts: ["书名"],
attr: "/book", // string | null;也可直接 await query.url("href")。
attrs: ["/book"], // 每项可为 null。
html: "书名", // string | null。
htmls: ["书名"], // 每项可为 null。
};
// 文档关闭后,派生出来的所有节点/查询都失效;先提取普通数据,再释放文档。
/**
* 常见查询与批量提取。
* const first = query.at(0); // 仍是惰性查询;at(-1) 为最后一项。
* const count = await query.count();
* const exists = await query.exists();
* const ownText = await query.ownText();
* const wholeText = await query.wholeText();
* const outerHTML = await query.outerHTML();
* const url = await query.url("href"); // 按 baseURL 解析;不存在/不可解析为 null。
* const urls = await query.urls("href");
* const rows = await document.select("article").extract(fields, { offset: 0, limit: 50 });
*
* 默认自动分批提取所有匹配项;offset/limit 是总结果范围,不是内部批大小。
* 不为每项创建 HtmlNode;最多64字段、完整数组8 MiB;单条不超过256 KiB。
* 文档节点预算50,000,取消/关闭/提前退出会释放原生遍历状态。
* 若逐条处理大结果:
* for await (const row of document.select("article").iterate(fields, { signal })) {
* console.log(row.title); // 可 break;不是每行一次原生调用。
* }
* iterate 须在 html.use 回调内完成,不能随已关闭文档逃逸。
*/
const fields = {
title: "h2", // 字符串是 selector + text 的简写。
url: { selector: "a", value: "url", attribute: "href" },
body: { value: "ownText" }, // 不传 selector 即当前 article。
tags: { selector: ".tag", value: "text", all: true },
};
const rows = [
{ title: "标题", url: "https://example.invalid/item", body: "正文", tags: ["标签"] },
{ title: null, url: null, body: "", tags: [] }, // 缺失 null,存在但空字符串 "",多值无匹配 []。
];
// value 支持 text/ownText/wholeText/attr/html/outerHTML/url。
// parse、查询和 extract/iterate 的最后一个选项对象均可添加 signal。
// await document.select("article").extract(schema, { offset: 0, limit: 20, signal: controller.signal });
// CSS 最终结果按 limit 收集;文档内编译后的选择器采用有界缓存,关闭文档一起释放。
// XPath 原生求值仍可能构建完整节点集,不承诺所有查询都仅遍历 limit 个节点。