Skip to content

包函数

包级函数适合一次性调用场景,内部使用 sync.Pool 复用 Processor,无需手动管理生命周期。注意:池化 Processor 已禁用缓存与审计保留;需要缓存/统计/审计请用 html.New 创建独立 Processor。

内容提取

Extract

从 HTML 字节中提取内容,返回完整的 Result

go
func Extract(htmlBytes []byte, cfg ...Config) (*Result, error)

参数

参数类型说明
htmlBytes[]byteHTML 内容
cfg...Config可选配置,最多一个

示例

go
result, err := html.Extract(data)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result.Title, result.Text)

ExtractFromFile

从 HTML 文件提取内容。

go
func ExtractFromFile(filePath string, cfg ...Config) (*Result, error)

文本提取

ExtractText

仅提取纯文本内容。

go
func ExtractText(htmlBytes []byte, cfg ...Config) (string, error)

ExtractTextFromFile

从文件提取纯文本。

go
func ExtractTextFromFile(filePath string, cfg ...Config) (string, error)

上下文版本

所有函数都支持带 context.Context 的版本,用于取消和超时控制:

函数签名
ExtractWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) (*Result, error)
ExtractFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) (*Result, error)
ExtractTextWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) (string, error)
ExtractTextFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) (string, error)
go
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

result, err := html.ExtractWithContext(ctx, data)

输出格式

函数签名说明
ExtractToMarkdown(htmlBytes []byte, cfg ...Config) (string, error)HTML → Markdown
ExtractToMarkdownFromFile(filePath string, cfg ...Config) (string, error)文件 → Markdown
ExtractToMarkdownWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) (string, error)带上下文
ExtractToMarkdownFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) (string, error)文件 + 上下文
ExtractToJSON(htmlBytes []byte, cfg ...Config) ([]byte, error)HTML → JSON
ExtractToJSONFromFile(filePath string, cfg ...Config) ([]byte, error)文件 → JSON
ExtractToJSONWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) ([]byte, error)带上下文
ExtractToJSONFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) ([]byte, error)文件 + 上下文

详细用法和示例详见 输出格式

链接提取

函数签名说明
ExtractAllLinks(htmlBytes []byte, cfg ...Config) ([]LinkResource, error)提取所有链接
ExtractAllLinksFromFile(filePath string, cfg ...Config) ([]LinkResource, error)从文件提取链接
ExtractAllLinksWithContext(ctx context.Context, htmlBytes []byte, cfg ...Config) ([]LinkResource, error)带上下文
ExtractAllLinksFromFileWithContext(ctx context.Context, filePath string, cfg ...Config) ([]LinkResource, error)文件 + 上下文

详细用法和示例详见 链接提取

批量处理

函数签名说明
ExtractBatch(htmlContents [][]byte, cfg ...Config) *BatchResult批量提取
ExtractBatchWithContext(ctx context.Context, htmlContents [][]byte, cfg ...Config) *BatchResult带上下文
ExtractBatchFiles(filePaths []string, cfg ...Config) *BatchResult批量文件提取
ExtractBatchFilesWithContext(ctx context.Context, filePaths []string, cfg ...Config) *BatchResult文件 + 上下文

详细用法和示例详见 批量处理